Php 使用Laravel 6上传图像

Php 使用Laravel 6上传图像,php,laravel,image-uploading,laravel-6,Php,Laravel,Image Uploading,Laravel 6,我试图上传一张图片,但当我提交表单时,什么也没发生。最后,数据库中有一个空的图像文件夹和一个nullimage字段。到目前为止,我已经在网上尝试了许多可能的解决方案 控制器 类报告控制器扩展控制器 { /** *只有登录的用户才能访问报告。 */ 公共函数构造() { $this->middleware('auth'); } 公共职能指数() { $reports=Report::with(['Victor','lost_vehicle'])->get(); 返回视图('reports.inde

我试图上传一张图片,但当我提交表单时,什么也没发生。最后,数据库中有一个空的图像文件夹和一个
null
image字段。到目前为止,我已经在网上尝试了许多可能的解决方案

控制器

类报告控制器扩展控制器
{
/**
*只有登录的用户才能访问报告。
*/
公共函数构造()
{
$this->middleware('auth');
}
公共职能指数()
{
$reports=Report::with(['Victor','lost_vehicle'])->get();
返回视图('reports.index')->带有('reports',$reports);
}
公共函数create()
{
返回视图('reports.create');
}
公共函数存储(ReportRequest$request)
{
$userid=Auth::user()->id;
请求()->验证([
//'image'=>'必需| image | mimes:jpeg、png、jpg、gif、svg',
]);
$Victor=Victor::创建([
'vic_name'=>$request->input('name'),
'vic_station'=>$request->input('statment'),
'case_id'=>uniqid(),
“报告状态”=>$request->input('status'),
'user_id'=>$userid,
]);
如果($files=$request->file('image')){
$carImage=time().。$files->getClientOriginalExtension();
$request->image->move(公共路径('images'),$carImage);
$Victor->lost_vehicles()->创建([
“图像”=>$carImage,
]);
}
$Victor->lost_vehicles()->创建([
'type'=>$request->input('type'),
'model'=>$request->input('model'),
“生产年”=>$request->input('year'),
'color'=>$request->input('color'),
'value'=>$request->input('value'),
'marks'=>$request->input('marks'),
“车牌号”=>$request->input('plate'),
“机箱号”=>$request->input('chassis'),
“最后位置”=>$request->input('location'),
'last_time_spotted'=>$request->input('last_time_spotted'),
“受害者id”=>$Victor->id,
]);
返回dd(受害者);
}
}

我将表单设置为接受文件
{!!表单::打开(['url'=>'reports'],数组('files'=>true))!!}

这部分代码有问题:

if($files=$request->file('image')){
...
$Victor->lost_vehicles()->创建([
“图像”=>$carImage,
]);
}
$Victor->lost_vehicles()->创建([
...
]);
您正在正确地检查是否存在
$request->file('image')
,但您调用了
$victor->lost_vehicles()->create()
两次。如果
lost\u vehicles
是一个
hasMany()
关系,那么这很好,但是您将在数据库中得到两条记录,一条记录有图像但没有信息,另一条记录将包含所有信息而没有图像。考虑重构:

$image=null;
如果($files=$request->file('image')){
$carImage=time().。$files->getClientOriginalExtension();
$request->image->move(公共路径('images'),$carImage);
$image=$carImage;
}
$Victor->lost_vehicles()->创建([
'type'=>$request->input('type'),
'model'=>$request->input('model'),
“生产年”=>$request->input('year'),
'color'=>$request->input('color'),
'value'=>$request->input('value'),
'marks'=>$request->input('marks'),
“车牌号”=>$request->input('plate'),
“机箱号”=>$request->input('chassis'),
“最后位置”=>$request->input('location'),
'last_time_spotted'=>$request->input('last_time_spotted'),
“受害者id”=>$Victor->id,
'image'=>$image
]);
这样做的目的是在开始时将
$image
设置为
null
,然后如果检查通过,则将其设置为表示上载图像的字符串。然后,它包含在下面的
create()
方法中,您应该将所有数据关联起来

除此之外,我还没有看到上传图像的方法。我使用:

\Storage::disk(“local”)->putFileAs(“images”,$files,$carImage);
//修改以适合您的变量名

还要确保您已调用
php artisan storage:link
storage/app
正确链接到
public

我完全按照您所说的那样做了,但图片名称仍然没有保存在数据库中,文件也没有移动到存储中!隐马尔可夫模型。。。你有什么错误吗?我认为如果没有相关的错误(由于权限、位置无效等原因无法移动文件),就不会发生这种情况。没有任何错误,所有字段都会插入到数据库中,除了image fieldNope,这行不应该是问题。我看不出
Form::file()
调用有任何错误,但我个人不使用
Form::
方法。它是否生成以下HTML:
?另外,您的
元素是否具有
enctype=“multipart/form data”
?(你必须从浏览器中检查页面才能看到计算出的HTML)嗯,我想这就是
'files'=>true
条目的作用。应该在同一个数组中吗
{!!Form::open(['url'=>'reports','files'=>true])!!}
Form::open
仅接受1个参数