[英]Laravel updateOrCreate And Increment Value
In my app, i'm saving customer interests depending on viewing products, adding to carts, orders and category viewing. 在我的应用中,我根据查看产品,添加到购物车,订单和类别查看来节省客户的兴趣。
Now here is my customerInterestController 现在这是我的customerInterestController
$customerInterest = [
'user_id' => Auth::user()->id,
'interest' => $category_id,
];
$data = [
'cart_interest' => ($column == 'cart') ? 1 : 0,
'order_interest' => ($column == 'order') ? 1 : 0,
'category_interest' => ($column == 'category') ? 1 : 0,
'view_interest' => ($column == 'view') ? 1 : 0,
];
try{
(new CustomerInterest)->insertCustomerInterest($customerInterest, $data);
}catch(Exception $e){
dd($e);
}
And here is my customerInterest Model 这是我的客户兴趣模型
public function insertCustomerInterest($customerInterest = [], $data = []){
return $this->updateOrCreate($customerInterest, $data);
}
There is no problem with inserting database. 插入数据库没有问题。 It works. 有用。
My Table
id
user_id
interest
cart_interest
order_interest
category_interest
view_interest
user_id and interest columns are composite unique index. user_id和interest列是复合 唯一索引。
If user_id, interest columns exists 如果user_id,则存在兴趣列
i want to update data 我想更新数据
if not i want to insert data. 如果没有,我想插入数据。
I use updateOrCreate method but couldn't increase the values in $data array. 我使用updateOrCreate方法,但无法增加$ data数组中的值。
How can i do that ? 我怎样才能做到这一点 ?
I solved my problem. 我解决了我的问题。
I removed $data array 我删除了$ data数组
Here is my code for insert or update 这是我的插入或更新代码
(new customerInterestController)->insertCustomerInterest('category', $category->id);
Here is my model 这是我的模特
public function insertCustomerInterest($customerInterest = [], $column){
return $this->updateOrCreate($customerInterest)->increment($column.'_interest');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.