【问题标题】:Laravel updateOrCreate And Increment ValueLaravel 更新或创建并增加值
【发布时间】:2015-10-15 07:48:25
【问题描述】:

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

现在这里是我的 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 数组中的值。

我该怎么做?

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    我解决了我的问题。

    我删除了 $data 数组

    这是我的插入或更新代码

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

    这是我的模型

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

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      相关资源
      最近更新 更多