英文:
I want to update record using laravel
问题
我尝试更新记录使用Laravel,我的记录成功更新了,但不是特定ID的记录在我点击更新按钮时更新,我该如何修复?
有人有任何想法?
控制器
public function updateaction(Request $request, $id)
{
$category = $request->input('select_category');
$project_name = $request->input('product_name');
$updaterecord = DB::table('projects')-
>where('products_id', $id)-
>update(['project_name' => $project_name, 'category_id' => $category]);
if ($updaterecord) {
return redirect('view_projects');
}
}
HTML视图
<form action="{{ route('project.update', $project->products_id) }}" method="POST">
@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="submit" class="btn btn-primary form-control" value="UPDATE" name="Update">
</div>
</form>
路由
Route::post('project_update/{id}/update','AdminController@updateaction')->name('project.update');
请使用以上修改后的代码。
英文:
I am trying to update record using laravel my record is updating successfully but not record updating of specific id when i click update button all record is updating how can i fix it ?
Does Anyone have any idea ?
controller
public function updateaction(Request $request,$id)
{
$category=$request->input('select_category');
$project_name=$request->input('product_name');
$updaterecord=DB::table('projects')-
>update(['project_name'=>$project_name,'category_id'=>$category]);
if($updaterecord)
{
return redirect('view_projects');
}
}
html view
<form action="{{route('project.update', $project->products_id)}}" method="POST"
>
@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="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');
答案1
得分: 1
**控制器**
你缺少 `->where(['id' => $id]);`
```php
public function updateaction(Request $request, $id)
{
$category = $request->input('select_category');
$project_name = $request->input('product_name');
$updaterecord = DB::table('projects')
->where(['id' => $id])->update(['project_name' => $project_name, 'category_id' => $category]);
if ($updaterecord) {
return redirect('view_projects');
}
}
在视图中
如果您使用 Resource Route
,请添加 @method('put')
<form action="{{ route('project.update', $project->products_id) }}" method="POST">
@csrf
@method('put')
<details>
<summary>英文:</summary>
**controller**
you are missing `->where(['id'=> $id]);`
```php
public function updateaction(Request $request,$id)
{
$category=$request->input('select_category');
$project_name=$request->input('product_name');
$updaterecord=DB::table('projects')
->where(['id'=> $id])->update(['project_name'=>$project_name,'category_id'=>$category]);
if($updaterecord){
return redirect('view_projects');
}
}
in View
add @method('put')
if you are using Resource Route
<form action="{{route('project.update', $project->products_id)}}" method="POST">
@csrf
@method('put')
答案2
得分: 0
你需要更改特定元素,因此使用该ID查找并更新该特定元素。
$project = Project::find($id);
$project->update(['project_name' => $project_name, 'category_id' => $category]);
英文:
You need to change the specific element so find with that id & update that specific element.
$project=Project::find($id);
$project->update(['project_name'=>$project_name,'category_id'=>$category]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论