Skip to content

Instantly share code, notes, and snippets.

@mzaman
Created September 30, 2016 12:08
Show Gist options
  • Select an option

  • Save mzaman/de1028f24b057a5bde9cb8c1651f722a to your computer and use it in GitHub Desktop.

Select an option

Save mzaman/de1028f24b057a5bde9cb8c1651f722a to your computer and use it in GitHub Desktop.
Laravel firstOrCreate would not update in case that the register already exists in the DB. Here the improved solution as we actually needed to update a table that has unique values not only for the column "id"
public static function createOrUpdate($data, $keys) {
$record = self::where($keys)->first();
if (is_null($record)) {
return self::create($data);
} else {
return $record->update($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment