gpt4 book ai didi

php - 带有 enctype 的表单在请求中返回空文件

转载 作者:行者123 更新时间:2023-12-04 07:40:31 27 4
gpt4 key购买 nike

我试图编辑我商店中的产品。根据要求,缺少 $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>
如果我删除带有图像的部分,则代码可以正常运行
我在 Controller 中的功能(类型资源)
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!");
}

最佳答案

这里的核心问题是value="{{ $product-> product_image }}"无效。 <input type="file">不支持,因为图片需要直接从用户的机器上传,除非选择并上传文件,$request->file('image')将是 null .
要处理这个清洁器,请在 Controller 中使用一些条件逻辑:
首先,上传图像并设置对文件的引用:

$profileImage = null;
if ($image = $request->file('image')) {
$destinationPath = 'storage/img/tech/';
$profileImage = $image->getClientOriginalName();
$image->move($destinationPath, $profileImage);
}
接下来,设置 $product->product_image基于 $profileImage 的值:
$product = Product::find($id);
...
if ($profileImage)
$product->product_image = $profileImage;
}
一个 ternarynull-coalesce可以使用语句,但它看起来有点奇怪:
$product->product_image = $profileImage ? $profileImage : $product->product_image;
// OR
$product->product_image = $profileImage ?? $product->product_image;
这两种情况都会设置 $product->product_image$profileImage ,或 $product->profile_image 的现有值如果未提供任何内容(将是现有图像或 null)

关于php - 带有 enctype 的表单在请求中返回空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67506033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com