繁体   English   中英

Laravel更新或创建并增加价值

[英]Laravel updateOrCreate And Increment Value

在我的应用中,我根据查看产品,添加到购物车,订单和类别查看来节省客户的兴趣。

现在这是我的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);
   }

这是我的客户兴趣模型

public function insertCustomerInterest($customerInterest = [], $data = []){
    return $this->updateOrCreate($customerInterest, $data);
}

插入数据库没有问题。 有用。

My Table

id
user_id
interest
cart_interest
order_interest
category_interest
view_interest

user_idinterest列是复合 唯一索引。

如果user_id,则存在兴趣列

我想更新数据

如果没有,我想插入数据。

我使用updateOrCreate方法,但无法增加$ data数组中的值。

我怎样才能做到这一点 ?

我解决了我的问题。

我删除了$ data数组

这是我的插入或更新代码

    (new customerInterestController)->insertCustomerInterest('category', $category->id);

这是我的模特

public function insertCustomerInterest($customerInterest = [], $column){
        return $this->updateOrCreate($customerInterest)->increment($column.'_interest');
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM