Laravel: adding a field in a column to existing data

How can I express this mysql php update code in eloquent

mysql_query("UPDATE `some_table` SET `value` = `value` + 1000 WHERE `id` = 1"); 

or

 mysql_query("UPDATE `some_table` SET `value` = `value` + $formdata WHERE `id` = 1"); 
+4
source share
3 answers

You can just get the model and enlarge it:

 $model = Some_Model::find( $id ); $model->value += 1000; $model->save(); 
+3
source

The ideal way to do this is to use the built-in Laravel increment function

 $model = Some_Model::find( $id ); $model->increment('value',1000); 

or

 Some_Model::where('id',1)->increment('value',1000); 

The documentation at the same address is at http://laravel.com/docs/queries#raw-expressions

+8
source

Using Eloquent, you can write your queries as follows:

 SomeTable::where('id', 1) ->update(array('value', DB::raw('value + 1000'))); 
+4
source

Source: https://habr.com/ru/post/1496693/


All Articles