php Laravel 中的增量列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29816123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increment columns in laravel
提问by Vainglory07
Is there a way to increment more than one column in laravel?
有没有办法在laravel中增加一列以上?
Let's say:
让我们说:
DB::table('my_table')
->where('rowID', 1)
->increment('column1', 2)
->increment('column2', 10)
->increment('column3', 13)
->increment('column4', 5);
But this results to:
但这导致:
Call to a member function increment() on integer
Call to a member function increment() on integer
I just want to find an efficient way to do this using the given functions from laravel. Thanks. Any suggestions will do.
我只想找到一种使用 laravel 给定函数的有效方法。谢谢。任何建议都可以。
回答by lukasgeiter
There is no existing function to do this. You have to use update()
:
没有现有的功能可以做到这一点。你必须使用update()
:
DB::table('my_table')
->where('rowID', 1)
->update([
'column1' => DB::raw('column1 + 2'),
'column2' => DB::raw('column2 + 10'),
'column3' => DB::raw('column3 + 13'),
'column4' => DB::raw('column4 + 5'),
]);
回答by Praveen AK
Increments and Decrements in Laravel Eloquent Model
Laravel Eloquent 模型中的增量和减量
Add to cart option is one of the most important functions in e-commerce websites. The tricky part is getting the number of items in the cart to display on the cart icon. The predominant approach to get this done is using the increment and decrement function on Laravel. This also facilitates the addition or removal of a product from your cart. The way to implement this function is ,
添加到购物车选项是电子商务网站中最重要的功能之一。棘手的部分是让购物车中的商品数量显示在购物车图标上。完成这项工作的主要方法是在 Laravel 上使用 increment 和 decrement 函数。这也有助于从购物车中添加或删除产品。实现这个功能的方法是,
$user = User::find(‘517c43667db388101e00000f');
$user->cart_count++;
// $user->cart_count--; // for decrement the count
$user->save()
An alternate and easier way is,
另一种更简单的方法是,
$user = User::find($article_id);
$user->increment('cart_count');
Also these will work:
这些也将起作用:
$user->increment('cart_count');// increase one count
$user->decrement('cart_count'); // decrease one count
$user->increment('cart_count',10); // increase 10 count
$user->decrement('cart_count',10); // decrease 10 count
回答by Staffan Storm Enemark
For future reference in 5.2 it has been made do able by doing the following
为了将来在 5.2 中参考,通过执行以下操作可以做到
You may also specify additional columns to update during the operation:
您还可以在操作期间指定要更新的其他列:
DB::table('users')->increment('votes', 1, ['name' => 'John']);
DB::table('users')->increment('votes', 1, ['name' => 'John']);
回答by Luceos
First off, the result of increment
is an integer according to the documentation: http://laravel.com/api/4.2/Illuminate/Database/Query/Builder.html
首先,increment
根据文档,结果是一个整数:http: //laravel.com/api/4.2/Illuminate/Database/Query/Builder.html
So you would have to do a call for each increment:
因此,您必须为每个增量调用一次:
DB::table('my_table')
->where('rowID', 1)
->increment('column1', 2);
DB::table('my_table')
->where('rowID', 1)
->increment('column2', 10);
DB::table('my_table')
->where('rowID', 1)
->increment('column3', 13);
DB::table('my_table')
->where('rowID', 1)
->increment('column4', 5);
I'm unable to find any quicker solution, unless you want to solve it with a raw update query command.
我找不到任何更快的解决方案,除非您想使用原始更新查询命令来解决它。
Also your example code will probably generate an error as you've closed the statement with ;
and continue with a new ->increment
call on the next line.
此外,您的示例代码可能会在您关闭语句;
并->increment
在下一行继续进行新调用时生成错误。
回答by Ziaur Rahman
Now in laravel 5.7 laravel query builder, increment and decrement, it can be done easily.
现在在 laravel 5.7 laravel 查询构建器中, increment 和 decrement,它可以轻松完成。
Model::where('id', "rowID")->increment('columne1');`
or you can use DB
或者你可以使用数据库
`DB::table("my_table")->where('id', "rowID")->increment('column1');