【发布时间】:2023-04-06 07:43:01
【问题描述】:
我尝试编辑商店中的产品。根据要求,缺少 $request->file('image'); 我在下面附上源代码,我真的不知道为什么我没有收到请求中的图像,因为我认为我所做的是正确的
我的表格:
<form method="POST" action="{{ route('products.update', $product->id)}}" enctype="multipart/form-data">
@csrf
@method('PATCH')
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput1">Product slug</label>
<input type="text" name="product_slug" value="{{$product->product_slug}}" class="form-control" id="exampleFormControlInput1" placeholder="Enter slug">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput111">Product title</label>
<input type="text" name="product_title" value="{{$product->product_title}}" class="form-control" id="exampleFormControlInput111" placeholder="Enter slug">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput2">Product category</label>
<input type="text" name="product_category" value="{{$product->product_category}}" class="form-control" id="exampleFormControlInput2" placeholder="name@example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput3">Product brand</label>
<input type="text" name="product_brand" value="{{$product->product_brand}}" class="form-control" id="exampleFormControlInput3" placeholder="name@example.com">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput22">Product display</label>
<input type="text" name="product_display" value="{{$product->product_display}}" class="form-control" id="exampleFormControlInput22" placeholder="name@example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput34">Product ram</label>
<input type="text" name="product_ram" value="{{$product->product_ram}}" class="form-control" id="exampleFormControlInput34" placeholder="name@example.com">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput33">Product os</label>
<input type="text" name="product_os" value="{{$product->product_os}}" class="form-control" id="exampleFormControlInput33" placeholder="name@example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput333">Product camera</label>
<input type="text" name="product_camera" value="{{$product->product_camera}}" class="form-control" id="exampleFormControlInput333" placeholder="name@example.com">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput8">Product price</label>
<input type="text" name="product_price" value="{{$product->product_price}}" class="form-control" id="exampleFormControlInput8" placeholder="name@example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlFile1">Change product photo</label>
<input type="file" name="image" value="{{$product->product_image}}" class="form-control-file" id="exampleFormControlFile1">
<img src="/storage/img/tech/{{$product->product_image}}" style="width:300px" alt="product_image">
</div>
</div>
</div>
<div class="form-group">
<label for="short_description">Short description</label>
<textarea class="form-control" name="about_product" id="short_description" rows="10">{{$product->about_product}}</textarea>
</div>
<div class="form-group">
<label for="long_description">Long description</label>
<textarea class="form-control" name="product_description" id="long_description" rows="10">{{$product->product_description}}</textarea>
</div>
<input type="submit" value="Edit" class="btn btn-success" name="submit">
<a href="/admin/products" class="btn btn-primary">Go back</a>
</form>
如果我删除带有图像的部分,则代码功能完美 我在控制器中的功能(类型资源)
public function update(Request $request, $id)
{
dd($request->all());
$request->validate([
'product_slug' => 'required|max:100',
'product_title' => 'required|max:100',
'product_category' => 'required|max:100',
'product_brand' => 'required|max:100',
'product_image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'product_display' => 'required',
'product_camera' => 'required',
'product_ram' => 'required',
'product_os' => 'required',
'product_price' => 'required|max:100',
'about_product' => 'required',
'product_description' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'storage/img/tech/';
$profileImage = $image->getClientOriginalName();
$image->move($destinationPath, $profileImage);
$input['image'] = $profileImage;
} else {
unset($input['image']);
}
$product = Product::find($id);
$product->product_slug = $request->get('product_slug');
$product->product_title = $request->get('product_title');
$product->product_category = $request->get('product_category');
$product->product_brand = $request->get('product_brand');
$product->product_display = $request->get('product_display');
$product->product_ram = $request->get('product_ram');
$product->product_camera = $request->get('product_camera');
$product->product_os = $request->get('product_os');
$product->product_price = $request->get('product_price');
$product->product_image = $profileImage;
$product->about_product = $request->get('about_product');
$product->product_description = $request->get('product_description');
$product->update();
return redirect('/admin/products')->with('success', "product updated!");
}
【问题讨论】:
-
所以请注意
<input type="file">不支持value="{{ $product->product_image }}",您必须实际选择要上传的文件才能显示该文件。 -
如果我想编辑一些东西,我该怎么做?
-
进行编辑的用户需要从他们的机器上传文件,或者您需要处理他们根本不上传文件的可能性。您看起来好像在处理这个问题,但如果没有上传文件,则未定义
$profileImage,因此您必须执行$product->product_image = isset($profileImage) ? $profileImage : null;,或使用if(isset($profileImage) { $product->product_image = $profileImage }包装该行 -
您提出了一个非常好的想法,但我最初并未分析。谢谢
-
添加为一般答案,以便您将其标记为正确
标签: php laravel file-upload laravel-8