Use the "wasRecentlyCreated" model attribute to check if the model was created or found

Published March 2, 2020 • 1 min read

Good to know, if you want to find out if a model was newly created or found:

Didn’t know about the wasRecentlyCreated model attribute to check if the model was created or found:

$dog = Dog::firstOrCreate([
    'name' => 'Barkalot'
]);

if ($dog->wasRecentlyCreated) {
    // Welcome the new dog
} else {
    // We already know the cute dog
}


laravel

Join my mailing list