英文:
How to update an image using Laravel?
问题
Here is the translated content without the code:
我想使用Laravel更新数据库中的图像,但不幸的是,图像没有更新。我该如何解决这个问题?
Controller
public function updateaction(Request $request, $id)
{
$category = $request->input('select_category');
$project_name = $request->input('product_name');
$image = $request->file('select_file');
$new_name = mt_rand().'.'.$image->getClientOriginalExtension();
$image->move(public_path('projects'), $new_name);
$updaterecord = DB::table('projects')->where(['products_id' => $id])->update([
'project_name' => $project_name, 'category_id' => $category, 'image' => $image
]);
if ($updaterecord) {
return redirect('view_project');
}
return back();
}
View
<form action="{{route('project.update', $project->products_id)}}" method="POST"
enctype="multipart/form-data">
@csrf
<div class="group-form">
<select class="form-control" name="select_category" required>
<option value="">Select Category</option>
@foreach($categories as $category)
<option value="{{$category->id}}" @if($project->category_id == $category->id) selected @endif>
{{$category->category_name}}</option>
@endforeach
</select>
</div>
<br>
<div class="group-form">
<input type="text" class="form-control" value="{{$project->project_name}}"
placeholder="Update Product Name" name="product_name" required>
</div>
<br>
<div class="group-form">
<input type="file" class="form-control" value="{{asset('/public/projects/'.$projects->image)}}"
name="select_file">
</div>
<br>
<div class="group-form">
<input type="submit" class="btn btn-primary form-control" value="UPDATE" name="Update">
</div>
</form>
Route
Route::post('project_update/{id}/update', 'AdminController@updateaction')
->name('project.update');
英文:
I want to update an image in my database using Laravel but unfortunately, the image is not updating. How can I resolve this issue?
Controller
<!-- language: php -->
public function updateaction(Request $request, $id)
{
$category = $request->input('select_category');
$project_name = $request->input('product_name');
$image = $request->file('select_file');
$new_name = mt_rand().'.'.$image->getClientOriginalExtension();
$image->move(public_path('projects'), $new_name);
$updaterecord = DB::table('projects')->where(['products_id' => $id])->update([
'project_name' => $project_name, 'category_id' => $category, 'image' => $image
]);
if ($updaterecord) {
return redirect('view_project');
}
return back();
}
View
<!-- language: php -->
<form action="{{route('project.update', $project->products_id)}}" method="POST"
enctype="multipart/form-data">
@csrf
<div class="group-form">
<select class="form-control" name="select_category" required>
<option value="">Select Category</option>
@foreach($categories as $category)
<option value="{{$category->id}}" @if($project->category_id) == $category->id) @endif>
{{$category->category_name}}</option>
@endforeach
</select>
</div>
<br>
<div class="group-form">
<input type="text" class="form-control" value="{{$project->project_name}}"
placeholder="Update Product Name" name="product_name" required>
</div>
<br>
<div class="group-form">
<input type="file" class="form-control" value="{{asset('/public/projects/'.$projects->image)}}"
name="select_file">
</div>
<br>
<div class="group-form">
<input type="submit" class=" btn btn-primary form-control" value="UPDATE" name="Update">
</div>
</form>
Route
<!-- language: php -->
Route::post('project_update/{id}/update', 'AdminController@updateaction')
->name('project.update');
答案1
得分: 2
尝试将 UploadedFile 对象保存到数据库。我认为你需要保存一个字符串作为文件名,例如,$new_name,而不是 $image。
英文:
You try to save UploadedFile object to database. I think, you need to save a string with file name, for example, $new_name instead of $image.
答案2
得分: 1
你的模型没有更新 'image' 属性,因为你在其中分配了一个对象 ($image
) 而不是字符串 ($new_name
)。
尝试在 UPDATE()
方法内用 $new_name
替换 $image
变量。
请参考以下代码:
public function updateaction(Request $request, $id)
{
$category = $request->input('select_category');
$project_name = $request->input('product_name');
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('projects'), $new_name);
$updaterecord = DB::table('projects')->where(['products_id' => $id])
->update(['project_name' => $project_name, 'category_id' => $category, 'image' => $new_name]);
if ($updaterecord) {
return redirect('view_project');
}
}
英文:
YOUR MODEL IS NOT UPDATING THE 'image' ATTRIBUTE BECAUSE YOU ASSIGN TO IT AN OBJECT ($image
) INSTEAD OF A STRING ($new_name
).
TRY TO REPLACE $image
VARIABLE INSIDE THE UPDATE()
METHOD WITH $new_name
.
SEE :
public function updateaction(Request $request,$id)
{
$category=$request->input('select_category');
$project_name=$request->input('product_name');
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('projects'), $new_name);
$updaterecord=DB::table('projects')->where(['products_id'=> $id])
->update(['project_name'=>$project_name,'category_id'=>$category,'image'=>$new_name]);
if($updaterecord){
return redirect('view_project');
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论