Laravel Update Multiple Records At Once

Update multiple rows of database in Laravel

Here is an easy way to do it.

$values = Value::where('project_id', $id)->update(['data'=>$data]);

I found it from this link

Hope it helps.

Update multiple rows at once Laravel Eloquent

Instead retrieving result, looping through, you can update directly,

$yes =  Tour::whereIn('id', $request->promote)->update(['promote' => 1]);
$no = Tour::whereNotIn('id', $request->promote)->update(['promote' => 0]);

How to update multiple rows in existing table at once in laravel 8?

You are getting error because you are trying to update the collection of stdClass objects, which you got from the database.

Try like this:

DB::table('data')
->where('category_id', '=', 1)
->update([
'subject' => $request->get('subject') ?? null,
'grade' => $request->get('grade') ?? null
]);
Flash::success('updated successfully');

return redirect('marksheet');

This will update your records in the database. For the security of valid insert in the database, you can use database-transactions.

You should read about update() in the docs.

Also, using models instead of Query builder is easier. You can read about that in the docs.

Updating Multiple Records using Eloquent (Laravel 5.4)

Eloquent equivalent is almost the same:

Exercise::whereIn('id', $ids)->update($request->all());

Update multiple values at once in Laravel using Eloquent

You can use eloquent to update multiple rows at once, either using a model or otherwise. For example:

Model::where($conditions)->update($newValues);

or

DB::table('users')->where($conditions)->update($newValues);

I would suggest taking a look at the Laravel documentation, https://laravel.com/docs/7.x/eloquent



Related Topics



Leave a reply



Submit