Laravel updateOrCreate和Increment Value

时间:2015-10-15 07:48:26

标签: php laravel eloquent

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

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

这是我的customerInterest模型

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_id 兴趣列是复合 唯一索引。

如果user_id,感兴趣列存在

我想更新数据

如果不是我想插入数据。

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

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我解决了我的问题。

我删除了 $ data 数组

这是我的插入或更新代码

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

这是我的模特

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