1

In my app, i'm saving customer interests depending on viewing products, adding to carts, orders and category viewing.

Now here is my 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.

If user_id, interest columns exists

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.

How can i do that ?

Cihan Küsmez
  • 1,967
  • 5
  • 22
  • 41

1 Answers1

4

I solved my problem.

I removed $data array

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');
    }
Cihan Küsmez
  • 1,967
  • 5
  • 22
  • 41