![](/img/trans.png)
[英]How to periodically update a database table's column for a million records in Laravel?
[英]Laravel - How to update data calculate by it's column
如何通過按列計算來更新數據我有表格項
+---------+-------+-------+-----------+
| item_id | stock | price | name |
+---------+-------+-------+-----------+
| 1 | 10 | 9.4 | Paper |
+---------+-------+-------+-----------+
| 2 | 10 | 12.8 | Pencil |
+---------+-------+-------+-----------+
| 3 | 10 | 99.9 | Note Book |
+---------+-------+-------+-----------+
然后我想通過計算庫存列來更新襪子
stock - $request->qty
如果我手動進行基本查詢
update items set stock = stock - 3 where item_id = 1
它正常工作,但是當我使用laravel雄辯地將股票變成0(零)時,我不知道為什么,
這是我的控制器
public function saveOrder(Request $request) {
$item = new Item;
$item->where('item_id', '=', $request->item_id)->update(['stock' => 'stock - '. $request->qty]);
return redirect('order');
}
有什么辦法嗎? 感謝您的回答,對不起,英語不好:)
試試這個:
$item = new Item;
$item->where('item_id', '=', $request->item_id)->decrement('stock', $request->qty);
您可以嘗試這樣的事情:
$item = Item::where('item_id', '=', $request->item_id)->first();
$item->stock = (int)$item->stock - (int)$request->qty;
$item->save();
或者,您可以嘗試使用DB :: raw表達式 。
這應該為您解決問題
public function saveOrder(Request $request) {
Item::where('item_id', $request->item_id)
->decrement('stock', $request->qty);
return redirect('order');
}
public function saveOrder(Request $request){
$stockdetails = \DB::table('tablename')->where('itemid',$request->itemid)->first(['stock as stock']);
//update query
$update= \DB::table('tablename')->update(['stock'=>($stockdetails->stock)-($request->quantity)])->where(itemid',$request->itemid);
if($update){
//sucecss
}else{
//Fail
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.