Laravel 在 null 上调用成员函数 getClientOriginalExtension()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37124454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Call to a member function getClientOriginalExtension() on null
提问by velvt
Hello im getting the following error while upload an image using laravel: "Call to a member function getClientOriginalExtension() on null".
您好,我在使用 laravel 上传图像时收到以下错误:“在 null 上调用成员函数 getClientOriginalExtension()”。
Here is my controller:
这是我的控制器:
$imageName = rand(11111, 99999) . '.' . $request->file('image')->getClientOriginalExtension();
$destinationPath = 'events';
$fileName = rand(11111, 99999) . '.' . $extension;
$upload_success = $image->move($destinationPath, $imageName);
Here is my view:
这是我的观点:
{!! Form::file('image', null, ['class' => 'form-control']) !!}
How do i save the $imageName to the pic field in my database. I tried this but it doesn't work. The field remains empty in the table.
如何将 $imageName 保存到数据库中的 pic 字段。我试过这个,但它不起作用。该字段在表中保持为空。
$task=$request->user()->tasks()->create([
'name' => $request->name,
'description' => $request->description,
'location' => $request->location,
'pic' => $imageName,
]);
回答by Camilo Rojas
in your form:open
you need the 'files' => true
like below
在你form:open
你需要'files' => true
像下面这样
Form::open('your_path', array('files'=> true))
or
或者
<form action="yout path" method="post" enctype="multipart/form-data">
回答by goodnesskay
This means that no file input has been seen for that file. So, check that you input something in each file input
这意味着没有看到该文件的文件输入。因此,请检查您是否在每个文件输入中输入了一些内容
回答by RashedRahat
Just simply add the following code inside your form's opening tag:
enctype="multipart/form-data"
只需在表单的开始标记中添加以下代码:
enctype="multipart/form-data"