照片无法使用 laravel 5.7 上传



之前使用 Laravel 5.6,我的照片上传得很完美,现在我已经升级到 5.7,现在他们不会,我不知所措。帖子会上传,只是不会上传照片。我已经检查了任何重新检查的关系和路线,但无济于事。任何帮助将不胜感激。

首页刀片.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>

帖子控制器.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');
}

照片.php:

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Photo extends Model
{
protected $fillable = [
'file',
];
public function user() {
return $this->belongsTo('AppUser');
}
public function photo() {
return $this->belongsTo('AppPhoto');
}
}

邮编.php:

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
protected $fillable = [
'body', 'photo_id', 'user_id',
];
public function post()
{
return $this->belongsTo('AppUser');
}
}

enctype="multipart/form-data"添加到<form>标签。当您使用具有文件上载控件的表单时,此值是必需的。

<form method="POST" action="{{ route('makePost') }}" enctype="multipart/form-data">
//
</form>

来源: HTML enctype 属性

最新更新