Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Addition to an existing field data in a column

Please how do i express this php mysql 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");
like image 775
Arr Yor Avatar asked Aug 13 '13 00:08

Arr Yor


3 Answers

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

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

or

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

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

like image 83
Abishek Avatar answered Nov 10 '22 21:11

Abishek


Using Eloquent you can write your queries as follow:

SomeTable::where('id', 1)
    ->update(array('value', DB::raw('value + 1000')));
like image 11
Rubens Mariuzzo Avatar answered Nov 10 '22 23:11

Rubens Mariuzzo


You can just retrieve the model and increment it:

$model = Some_Model::find( $id );
$model->value += 1000;
$model->save();
like image 9
kodepup Avatar answered Nov 10 '22 22:11

kodepup