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 ?

1 Answer 1

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');
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.