Skip to content

Instantly share code, notes, and snippets.

@rstriquer
Last active August 26, 2021 13:08
Show Gist options
  • Save rstriquer/2bf8fb693fdd5cb868489ad937dda370 to your computer and use it in GitHub Desktop.
Save rstriquer/2bf8fb693fdd5cb868489ad937dda370 to your computer and use it in GitHub Desktop.
update model illuminate eloquent (laravel database)
# once i come accross this, seams to be a good thing to remember ..
# it creates a new record on database table and automatically replace objects attributes with the new created record
class fooModel
{
public function create(array $data)
{
if (!isset($data['id'])) {
$data['id'] = rand(0,999);
}
$new = new static;
$new = parent::create($data);
$this->attributes = $new->attributes;
$this->original = $new->original;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment