I was wondering how to perform something like this:
我想知道如何执行这样的事情:
Table::update(array('position'=>'position+1'));
As far as I know, laravel 4 handles 'position+1' as a string, thus is becomes 0.
据我所知,laravel 4将'position + 1'作为一个字符串处理,因此变为0。
I want to perform something like
我想表现得像
UPDATE table SET position = position + 1
Can I do that using eloquent?
我能用口才吗?
EDIT: nevermind, doh.."DB::table('users')->increment('votes');"
编辑:没关系,doh ..“DB :: table('users') - > increment('votes');”
4 个解决方案
#1
37
Simply make use of the increment
method:
只需使用增量方法:
DB::table('users')->increment('position');
The same is valid for decrement
:
同样适用于减量:
DB::table('users')->decrement('rank');
You may even set the second parameter to the amount you want to add/subtract:
您甚至可以将第二个参数设置为要添加/减去的数量:
DB::table('users')->increment('posts', 5);
DB::table('users')->decrement('likes', 3);
Also, if you need to update other columns along with it, you pass it as the third parameter:
此外,如果您需要更新其他列,请将其作为第三个参数传递:
DB::table('users')->increment('range', 3, array(
'name' => 'Raphael',
'rank' => 10
));
And the same goes for Eloquent
models, as well:
对于Eloquent模型也是如此:
$firstUser = User::find(1);
$firstUser->increment('height', 0.1, array(
'active' => false
));
#2
5
you can also do with DB::raw method like this:
您也可以使用DB :: raw方法,如下所示:
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position+1')]);
you can also do other operations with this like
你也可以用这个做其他操作
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position * 2')]);
#3
1
This worked fine for me
这对我来说很好
\Models\User::where("id", $userId)->increment("points");
#4
-1
simply you can use the DB::raw method like this: Table::update(DB::raw('position=position+1'));
你可以使用像这样的DB :: raw方法:Table :: update(DB :: raw('position = position + 1'));
#1
37
Simply make use of the increment
method:
只需使用增量方法:
DB::table('users')->increment('position');
The same is valid for decrement
:
同样适用于减量:
DB::table('users')->decrement('rank');
You may even set the second parameter to the amount you want to add/subtract:
您甚至可以将第二个参数设置为要添加/减去的数量:
DB::table('users')->increment('posts', 5);
DB::table('users')->decrement('likes', 3);
Also, if you need to update other columns along with it, you pass it as the third parameter:
此外,如果您需要更新其他列,请将其作为第三个参数传递:
DB::table('users')->increment('range', 3, array(
'name' => 'Raphael',
'rank' => 10
));
And the same goes for Eloquent
models, as well:
对于Eloquent模型也是如此:
$firstUser = User::find(1);
$firstUser->increment('height', 0.1, array(
'active' => false
));
#2
5
you can also do with DB::raw method like this:
您也可以使用DB :: raw方法,如下所示:
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position+1')]);
you can also do other operations with this like
你也可以用这个做其他操作
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position * 2')]);
#3
1
This worked fine for me
这对我来说很好
\Models\User::where("id", $userId)->increment("points");
#4
-1
simply you can use the DB::raw method like this: Table::update(DB::raw('position=position+1'));
你可以使用像这样的DB :: raw方法:Table :: update(DB :: raw('position = position + 1'));