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 ?