I have the following function which creates a new record in the database if one doesn't already exists - if one exists, it updates it. The problem is that it returns true
and therefore I can't get the ID of the inserted or updated record.
/**
* Save timesheet.
*
* @param $token
* @param $data
*/
public function saveTimesheet($token, $data)
{
return $this->timesheet->firstOrNew($token)->fill($data)->save();
}
First create the new model and then save it, the id will be set in the model automaticaly.
/**
* Save timesheet.
*
* @param $token
* @param $data
*/
public function saveTimesheet($token, $data)
{
// Set the data
$model = $this->timesheet->firstOrNew($token)->fill($data);
// Save the model
$model->save();
// Return the id
return $model->id;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With