簡體   English   中英

Laravel 5. FirstorNew有幾個條件

[英]Laravel 5. FirstorNew with several conditions

如果沒有具有相同product_namebrandweightvolume的產品,我想將產品添加到數據庫。

這是我的代碼:

$product = Product::firstorNew(['product_name'=>$request->product)], ['brand'=>$reques->brand), ['weight'=>$request->weight], ['volume'=>$request->volume]);

$product->product_name = $request->product;
$product->brand = $request->brand;
$product->weight = $request->weight;
$product->volume = $request->volume;
$product->save();

此代碼不檢查所有屬性,只檢查第一個屬性,如果匹配,則更新行而不是添加新屬性。 例如,在DB中有一個產品id=1product_name='Milk'brand='Parmalat'weight='null'volume=1 ,如果我添加product_name='Milk'brand='Unimilk'weight='null'volume=0.5 row with id = 1將被更新,但在這種情況下,我想向DB添加一個新行。

看起來您的括號放錯了。 嘗試這個:

$product = Product::firstOrNew([
  'product_name' => $request->product,
  'brand' => $request->brand,
  'weight' => $reqeust->weight,
  'volume' => $request->volume,
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM