我从laravel的5.4集体形式转换为5.7形式。一切工作正常,直到我切换并尝试使用新表格。我正在尝试上传带有文字帖子的简单图片。文本已上载到数据库中,但图片/文件未上载。我不确定我缺少什么,因此如果有人可以提供帮助,我们将不胜感激。预先感谢。
home.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<form method="POST" enctype="multipart/form-data" 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-md-6">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body') }}">
</div>
</div>
<div class="form-group row">
<label for="photo" class="col-md-4 col-form-label text-md-right">{{ __('Photo') }}</label>
<div class="col-md-6">
<input id="photo" type="file" class="form-control" name="photo" value="{{ old('photo') }}">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Submit Post') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
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');
}
答案 0 :(得分:0)
问题在这里:
if($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
这里假设输入的名称为photo_id
,但您在表单中将名称设置为photo
:
<input id="photo" type="file" class="form-control" name="photo" value="{{ old('photo') }}">
因此,您应该保持一致-在所有地方使用相同的名称-photo_id
或photo