php - Laravel updateOrCreate And Increment Value -
in app, i'm saving customer interests depending on viewing products, adding carts, orders , category viewing.
now here 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 customerinterest model
public function insertcustomerinterest($customerinterest = [], $data = []){ return $this->updateorcreate($customerinterest, $data); }
there no problem inserting database. works.
my table id user_id interest cart_interest order_interest category_interest view_interest
user_id , interest columns composite unique index.
if user_id, interest columns exists
i want update data
if not want insert data.
i use updateorcreate method couldn't increase values in $data array.
how can ?
i solved problem.
i removed $data array
here code insert or update
(new customerinterestcontroller)->insertcustomerinterest('category', $category->id);
here model
public function insertcustomerinterest($customerinterest = [], $column){ return $this->updateorcreate($customerinterest)->increment($column.'_interest'); }
Comments
Post a Comment