samedi 13 août 2016

Laravel save() is not update record

I am trying to update records to the database table if record is already exits. Insert new record if record is not to the database table.

I have write below code for that

DB::enableQueryLog();
            $user->userCommission()->save(new UserCommission($input['commission']));
            dd(DB::getQueryLog());

when EnableQueryLog it's always show insert query, If record is already in the table.

Here is my query..

array:1 [▼
  0 => array:3 [▼
    "query" => "insert into `user_commissions` (`created_by`, `status_id`, `percent`, `user_id`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)"
    "bindings" => array:6 [▼
      0 => "1"
      1 => "1"
      2 => "0.10"
      3 => 21
      4 => "2016-08-13 08:07:45"
      5 => "2016-08-13 08:07:45"
    ]
    "time" => 2.57
  ]
]

In above query user_id 21 record already in the table although Save() insert record to the database.

where I am wrong with this code? May I have to apply unique key to the table?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire