Created
September 30, 2016 12:08
-
-
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"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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