Php 在laravel 5.8中更新图像时文件输入表单的默认值
我使用的是Laravel 5.8,我试图在Laravel中更新数据,数据包含图像,当表单更新没有获得新图像时,Laravel显示错误,图像为空, 我可以将旧图像作为文件输入的默认值吗 这是我的控制器Php 在laravel 5.8中更新图像时文件输入表单的默认值,php,laravel,Php,Laravel,我使用的是Laravel 5.8,我试图在Laravel中更新数据,数据包含图像,当表单更新没有获得新图像时,Laravel显示错误,图像为空, 我可以将旧图像作为文件输入的默认值吗 这是我的控制器 public function UploadEdit(Request $request, Books $book,$id_book){ $book = Books::find($id_book); global $old_image; $old_image = public_
public function UploadEdit(Request $request, Books $book,$id_book){
$book = Books::find($id_book);
global $old_image;
$old_image = public_path("img/books/". $book->image);
if($request->hasFile('image')){
if (File::exists($old_image)) {
unlink($old image);
}
$image = $request->file('image');
$image_name = time()."_".$image->getClientOriginalName();
$image->move('img/books',$image_name);
$book->image=$image_name;
}else{
$book->image = $old_image;
}
DB::table('books')->where('id_book',$request->id_book,$image_name)->update([
'title' => $request->title,
'image' => $request->image = $image_name,
'category' => $request->category,
'description' => $request->description
]);
return redirect('/books');
}
这是我表格中的输入文件:
<div class="form-group">
<label for="image">Image</label>
<input type="file" class="form-control-file" id="image" name="image" id="image" enctype="multipart/form-data" value="{{ asset("img/books/$book->image") }}">
<img src="{{ asset("img/books/$book->image") }}" width="150">
</div>
形象
图像“}}”>
图像“}}”宽度=“150”>
我正在尝试为文件表单设置默认值,但仍然不起作用您不能为
设置默认值。您可以做的最好的事情是在更新books表时检查请求是否有图像如果没有,则可以跳过更新图像或重新分配上一个图像
这是它的样子
DB::table('books')->其中('id\u book',$request->id\u book,$image\u name)->更新([
'title'=>$request->title,
'image'=>$request->hasFile('image')?$image\u name:$book->image,
“类别”=>$request->category,
'description'=>$request->description
]);
您不能为
设置默认值。您可以做的最好的事情是在更新图书表时检查请求是否有图像,如果没有,则可以跳过更新图像或重新分配上一个图像
这是它的样子
DB::table('books')->其中('id\u book',$request->id\u book,$image\u name)->更新([
'title'=>$request->title,
'image'=>$request->hasFile('image')?$image\u name:$book->image,
“类别”=>$request->category,
'description'=>$request->description
]);
您不需要获取旧文件。如果在执行更新时未在数据库和存储中更改它,则它仍将存在,或者如果已更改,则仍将为null
您可以使用要从方法开始更新的数据准备数组,并仅在图像元素存在时添加图像元素
$data = [
'title' => $request->title,
'category' => $request->category,
'description' => $request->description
];
if($request->hasFile('image')){
$image = $request->file('image');
$image_name = time()."_".$image->getClientOriginalName();
$image->move('img/books',$image_name);
$data = array_merge($data, ['image' => $image_name]); // here you merge the image name on the data
}
DB::table('books')->where('id_book',$request->id_book)->update($data);
您不需要获取旧文件。如果在执行更新时未在数据库和存储中更改它,则它仍将存在,或者如果已更改,则仍将为null 您可以使用要从方法开始更新的数据准备数组,并仅在图像元素存在时添加图像元素
$data = [
'title' => $request->title,
'category' => $request->category,
'description' => $request->description
];
if($request->hasFile('image')){
$image = $request->file('image');
$image_name = time()."_".$image->getClientOriginalName();
$image->move('img/books',$image_name);
$data = array_merge($data, ['image' => $image_name]); // here you merge the image name on the data
}
DB::table('books')->where('id_book',$request->id_book)->update($data);