如何在laravel 6中更新图像
我想更新图像,我想如果图像被上传,图像将被更新,如果没有,它不会更新图像,那么之前的图像仍然存在。 在我的例子中,它给空的图像,它更新相同的图像是不上传的 annoncesController.php如何在laravel 6中更新图像,laravel,Laravel,我想更新图像,我想如果图像被上传,图像将被更新,如果没有,它不会更新图像,那么之前的图像仍然存在。 在我的例子中,它给空的图像,它更新相同的图像是不上传的 annoncesController.php public function update(Request $request, $id){ $file=File::find($id); $file->type = $request->type; if($request->has
public function update(Request $request, $id){
$file=File::find($id);
$file->type = $request->type;
if($request->hasFile('image'))
{
$path = $request->image->store('annonces');
$file->image = $path;
}
$file->update([
$file->type = $request->type,
$file->image = $request->image
]);
return Redirect::to("file")
->withSuccess('Great! file has been successfully uploaded.');
}
您正在if条件中设置新上载文件的路径,然后在update语句中用$request->image(可能是二进制图像数据)覆盖它。此外,正如注释中指出的,代码中几乎没有打字错误/语法错误 你可以试试下面的方法
公共功能更新(请求$Request,$id){
$file=file::find($id);
如果($request->hasFile('image')&&&$request->file('image')->isValid())
{
//如果需要,验证mime类型、文件大小等
//存储新上载的图像并获取其路径。
$path=$request->image->store('annonces');
//删除现有图像
存储::删除($file->image);
//更新新路径
$file->image=$path;
}
$file->type=$request->type;
$file->save();
返回重定向::到(“文件”)
->withSuccess('很好!文件已成功上载');
}
您设置了$file->image=$path
,但是在更新$file->image=$request->image
中。另外,当您在数组中赋值时,没有使用=>