我是laravel框架的初学者。我已经完成了所有的工作,但是当我尝试上传图像时,它会调用未定义的方法Illuminate \ Support \ Facades \ Request :: file()错误。我已导入所有必需的库。那么我没找到的错误是什么。
This is method for image upload and move
public function store(Request $request) {
$post = Request::all();
if ($file = $request->file('file_path')) {
$name = $file->getClientOriginalName();
$name->move('uploads', $name);
$post['file_path'] = $name;
}
Post::create($post);
return redirect('post');
}
这是我的用户输入表单。
<section>
<div class="container">
<div class="row col-md-6 col-md-offset-3">
<h3>Add your new post</h3>
{!! Form::open(['url' => 'post/store', 'files' => true]) !!}
<div class="form-group">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<select class="form-control" name="category">
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->category_name }}</option>
@endforeach
</select>
<div class="form-group">
{!! Form::label('file_path', 'Image') !!}
{!! Form::file('file_path', null) !!}
</div>
<div class="form-group">
{!! Form::label('published_at', 'Publish on') !!}
{!! Form::input('date','published_at', date('Y,m,d'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('Description', 'Description') !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('published_by', 'Published By') !!}
{!! Form::text('published_by', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Submit',['class' => 'btn btn-success']) !!}
</div>
{!! Form::close() !!}
当我在var_dump中打印$ post变量时,它会给出错误:
D:\wamp64\www\laravel\app\Http\Controllers\PostController.php:19:
array (size=7)
'_token' => string '5j1YVXV41NwjeKbTDlYJMrCXp39mECGTyVjhy3Jh' (length=40)
'title' => string 'Lorem Post' (length=10)
'category' => string '1' (length=1)
'published_at' => string '2016,06,11' (length=10)
'description' => string 'Lorem Ipsum Dolor Imet.' (length=23)
'published_by' => string 'LaraGeek' (length=8)
'file_path' =>
object(Illuminate\Http\UploadedFile)[166]
private 'test' (Symfony\Component\HttpFoundation\File\UploadedFile) => boolean false
private 'originalName' (Symfony\Component\HttpFoundation\File\UploadedFile) => string 'annapurna.jpg' (length=13)
private 'mimeType' (Symfony\Component\HttpFoundation\File\UploadedFile) => string 'image/jpeg' (length=10)
private 'size' (Symfony\Component\HttpFoundation\File\UploadedFile) => int 20076
private 'error' (Symfony\Component\HttpFoundation\File\UploadedFile) => int 0
private 'pathName' (SplFileInfo) => string 'D:\wamp64\tmp\php15A3.tmp' (length=25)
private 'fileName' (SplFileInfo) => string 'php15A3.tmp' (length=11)
答案 0 :(得分:4)
为了解决这个问题,您需要导入基础请求类,以便正确注入它。在文件顶部添加以下内容:
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile;
use File;
use Illuminate\Support\Facades\Input;