Home > Back-end >  laravel : how can i update multiple rows in one time?
laravel : how can i update multiple rows in one time?

Time:12-28

I want to select multiple rows in update them in one time by changing 1 attribute for all of them , I have no idea how can I do it , guys if you have a tutorial or video who explain it, share it with me !

screen

CodePudding user response:

In Laravel, something like this should suffice in your controller function:

DB::table('Contact List')
              ->where('id', '<=', $high_end)
              ->where('id', '>=', $low_end)
              ->update([
                      'First Name' => 'Sultan',
                      'Last Name' => 'Soleman',
                       ]);

CodePudding user response:

Put this code in your controller function

ModelName::whereIn("id",$idArr)->update([Ref' => 'By Google']);

$idArr is array of requested userId.

CodePudding user response:

Example ModelName=Person

Controller

use App\Models\Person;      
Person::query()->update(['column_name' => 'value']);

Or View

@php
 use App\Models\Person;      
 Person::query()->update(['column_name' => 'value']);
@endphp

This codes update to all rows.If you're just update selected columns on controller method.

you must define it at the top of the page Request class

use Illuminate\Http\Request; 
public function person_update(Request $request)
{
        $person_update= Person::find($request->ID);
        $person_update->Ref= $request->ref;
        $person_update->first_name= $request->first_name;
        $person_update->another_column_name= $request->another_value;
        $person_update->save();
}
  • Related