How to set every row to the same value with Laravel's Eloquent/Fluent?
Asked Answered
H

9

95

I need to update all of the rows in a database so that a particular field in all of them is equal to a single value. Here's an example.

Let's say my database table is like so:

id data confirmed
1 someData 0
2 someData 1
3 someData 0

I want to perform a query that sets the confirmed field of every row to 1.

I could do it this way:

$rows = MyModel::where('confirmed', '=', '0')->get();
foreach($rows as $row) {
    $row->confirmed = 0;
    $row->save();
}

But it seems like there would be a better way? A single query that would just say "set every row's 'confirmed' field to 1."

Does such a query exist in Laravel's Eloquent/Fluent?

Harberd answered 25/3, 2013 at 19:4 Comment(0)
S
102

Well, an easy answer: no, you can't with eloquent. A model represents 1 row in the database, it wouldn't make sense if they implemented this.

However, there is a way to do this with fluent:

$affected = DB::table('table')->update(array('confirmed' => 1));

or even better

$affected = DB::table('table')->where('confirmed', '=', 0)->update(array('confirmed' => 1));
Sleek answered 26/3, 2013 at 13:24 Comment(1)
This works, but the newer answer by Matt is little bit cleaner way to do it since that answer uses Eloquent so you don't have to be dependent on knowing the table name and resorting to the database builder functions.Physician
R
180

Just to keep this thread current, you can update all rows against an Eloquent model directly using:

Model::query()->update(['confirmed' => 1]);

If you are using something like WHERE

Model::where('foo', '=', 'bar')->update(['confirmed' => 1])

If you want to include soft deleted rows as well

Model::query()->withTrashed()->update(['confirmed' => 1]);
Rootstock answered 14/9, 2016 at 15:11 Comment(4)
I prefer to using Eloquent. this must be the answerEndways
This is the best way to do it. Just to add some explanation for the query() part, if you were doing something like Model::where('foo', '=', 'bar')->update(['confirmed' => 1]) you wouldn't need the query() part. But to update all rows without a where(), the query() is needed to get the query builder object.Physician
Note that this is different from DB::table('table') in that this doesn't take soft-deleted rows into account! For that you can use: Model::query()->withTrashed()->update(['confirmed' => 1]);Germanic
The accepted answer says "no, you can't with eloquent" which does not seem to be the case after this answer..Praseodymium
S
102

Well, an easy answer: no, you can't with eloquent. A model represents 1 row in the database, it wouldn't make sense if they implemented this.

However, there is a way to do this with fluent:

$affected = DB::table('table')->update(array('confirmed' => 1));

or even better

$affected = DB::table('table')->where('confirmed', '=', 0)->update(array('confirmed' => 1));
Sleek answered 26/3, 2013 at 13:24 Comment(1)
This works, but the newer answer by Matt is little bit cleaner way to do it since that answer uses Eloquent so you don't have to be dependent on knowing the table name and resorting to the database builder functions.Physician
V
28

You can do this with elquent (laravel 4):

MyModel::where('confirmed', '=', 0)->update(['confirmed' => 1])
Vaticinal answered 24/3, 2015 at 8:51 Comment(0)
P
3

This working for me :

   MyModel::query()->update(  ['confirmed' => 1] );
Parr answered 29/12, 2021 at 8:47 Comment(0)
I
2

Model::where('confirmed', 0)->update(['confirmed' => 1])

Immutable answered 14/10, 2021 at 9:13 Comment(0)
K
1

Solution for updating all rows :

  1. Create an extra column ( like 'updateAll') and assign static value for all rows (like this 'updateAll' = '1' ) in mysql table.

  2. Add hidden input field with name="forUpdateAll" and value="forUpdateAllValue" ( to execute only specific code for updating all rows)

  3. Then for update(Request $request, $id) method add this code :
public function update(Request $request, $id){
      if($request->get('forUpdateAll') == "forUpdateAllValue"){
                 $question = \App\YourModel::where('updateAll',$id)
                     ->update([
                         'confirmed' => 1
                     ]);

      }else {
          //other code ( update for unique record ) 
      }
 }
  1. Setup your form like this :
<form role="form" action="/examples/1" method="post">        
      {{ method_field('PATCH') }}
      {{ csrf_field()}}
      <input type="hidden" name="forUpdateAll" value="forUpdateAllValue">  
      <button type="submit" class="btn btn-primary">Submit</button>
  </form>
Korykorzybski answered 7/6, 2020 at 5:7 Comment(0)
F
1

Update any column fileds

DB::table('your_table_name')->update(['any_column_name' => 'any value']);
Fredette answered 20/12, 2021 at 17:25 Comment(0)
V
0

Update all fields using laravel eloquent:

Way -> 1

[Status-> Columns you want to update]

Model::where('status', '=', 1)->update(['status' => 0]);

Way -> 2

[Status-> Columns you want to update]

$allData = Model::where('status', 1)->get();
                foreach ($allData as $data){
                    $data->status= 0;
                    $data->update();
                }
Vitalis answered 25/3, 2013 at 19:4 Comment(0)
U
-8

You can do this to update all the records.

App\User::where('id', 'like', '%')->update(['confirmed' => 'string']);

Unlearn answered 12/6, 2018 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.