Reputation: 151
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');
Upvotes: 0
Views: 55
Reputation: 12391
controller
you are missing ->where(['id'=> $id]);
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')
Upvotes: 1
Reputation: 3704
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]);
Upvotes: 0