无法使用laravel 5.7上传照片

时间:2018-09-23 17:51:38

标签: php laravel

使用Laravel 5.6之前,我的照片可以完美上传,而现在我已升级到5.7,现在它们不再显示了,我很茫然。帖子将上传,但不会上传照片。我已经检查了任何重新检查的关系,并认为肠道无济于事。任何帮助将不胜感激。

home.blade.php:

<form method="POST" action="{{ route('makePost') }}">
                            @csrf

                            <div class="form-group row">
                                <label for="body" class="col-md-4 col-form-label text-md-right">{{ __('Body') }}</label>

                                <div class="col-lg-6">
                                    <textarea id="body" type="text" class="form-control{{ $errors->has('body') ? ' is-invalid' : '' }}" name="body" value="{{ old('body') }}" required autofocus></textarea>

                                    @if ($errors->has('body'))
                                        <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('body') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-md-6">
                                <input id="image" type="file" class="form-control{{ $errors->has('image') ? ' is-invalid' : '' }}" name="image" value="{{ old('image') }}" autofocus>

                                @if ($errors->has('image'))
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('image') }}</strong>
                                    </span>
                                @endif
                            </div>

                            <div class="form-group row mb-0">
                                <div class="col-md-6 offset-md-4">
                                    <button type="submit" class="btn btn-primary">
                                        {{ __('Create Post') }}
                                    </button>
                                </div>
                            </div>
                        </form>

PostsController.php:

public function store(Request $request)
{
    $input = $request->all();

    $user = Auth::user();

    if($file = $request->file('photo_id')) {
        $name = time() . $file->getClientOriginalName();
        $file->move('images', $name);
        $photo = Photo::create(['file'=>$name]);
        $input['photo_id'] = $photo->id;
    }

    $user->post()->create($input);
    return redirect('/home');
}

Photo.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{

    protected $fillable = [
        'file',
    ];

    public function user() {
        return $this->belongsTo('App\User');
    }

    public function photo() {
        return $this->belongsTo('App\Photo');
    }
}

Post.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'body', 'photo_id', 'user_id',
    ];

    public function post()
    {
        return $this->belongsTo('App\User');
    }
}

1 个答案:

答案 0 :(得分:1)

n=int(input("Enter the Value for n:")) result=1 for i in range(n, 1, -1): result=result*i print("factorial of",n,"is",result) 添加到enctype="multipart/form-data"标签。当您使用具有文件上传控件的表单时,此值是必需的。

<form>

来源:HTML enctype Attribute