提问人:eka dita 提问时间:11/12/2023 更新时间:11/13/2023 访问量:35
如何为新产品或现有产品添加库存的条件
How to make condition to add stock for new product or existing product
问:
我是Laravel的初学者,我想创建一个网站来管理库存 如果我想为已注册的产品添加库存,或者为尚未注册/还没有库存的产品添加新库存,我想创造条件
public function store(StoreStockGudangRequest $request)
{
$validated = $request->validated();
$product_check = StockGudang::where('product_id', $request->product_id)->where('size_id', $request->size_id)->where('color_id', $request->color_id)->get();
if($product_check == null){
StockGudang::create($validated);
}else{
StockGudang::where('product_id', $request->product_id)->where('size_id', $request->size_id)->where('color_id', $request->color_id)->increment('stock', $request->stock);
}
return redirect('/dashboard/stock')->with('success', 'Stock has been added!');
}
答:
0赞
Denis Sinyukov
11/13/2023
#1
public function store(StoreStockGudangRequest $request)
{
$validated = $request->validated();
$where = [
'product_id' => $request->product_id,
'size_id' => $request->size_id,
'color_id' => $request->color_id
];
$product_check = StockGudang::query()->where($where)->exists();
if(! $product_check){
StockGudang::create($validated);
} else {
StockGudang::query()->where($where)->increment('stock', $request->get('stock', 1));
}
return redirect('/dashboard/stock')->with('success', 'Stock has been added!');
}
评论