Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use firstOrNew to check if a record exists
    Laravel firstOrNew firstOrCreate Eloquent Laravel Eloquent record existence update record create record PHP


How can I use firstOrNew to check if a record exists


To use the `firstOrNew()` method to check if a record exists in Laravel, you can follow these steps:

php
// Check if a record exists
$user = User::firstOrNew(['email' => '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy97851 = '&#101;x&#97;mpl&#101;' + '&#64;';
 addy97851 = addy97851 + '&#101;m&#97;&#105;l' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy97851 + suffix + '\'' + attribs + '>' );
 document.write( addy97851 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>']);

if ($user->exists) {
    // Record exists
    // You can now update the record if needed
    $user->update([
        'name' => 'Updated Name'
    ]);
} else {
    // Record does not exist
    // You can now save the new record
    $user->save();
}

Here's how it works:

1. The `firstOrNew()` method is used to retrieve the first matching record based on the given conditions (`['email' => '
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
']`). If no record is found, it will return a new model instance with the given attributes.

2. After calling `firstOrNew()`, you can check the `$user->exists` property to determine if the record already exists in the database.

3. If the record exists (`$user->exists` is `true`), you can update the record as needed.

4. If the record does not exist (`$user->exists` is `false`), you can save the new record by calling `$user->save()`.

The advantage of using `firstOrNew()` is that it allows you to check if a record exists and then perform the appropriate action (update or create) in a single method call, without having to write additional conditional logic.

Alternatively, you can also use the `firstOrCreate()` method, which will create a new record if it doesn't exist:

php
$user = User::firstOrCreate(['email' => '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy81864 = '&#101;x&#97;mpl&#101;' + '&#64;';
 addy81864 = addy81864 + '&#101;m&#97;&#105;l' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy81864 + suffix + '\'' + attribs + '>' );
 document.write( addy81864 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>'], [
    'name' => 'New User'
]);

if ($user->wasRecentlyCreated) {
    // Record was just created
} else {
    // Record already existed
}

In this case, the `firstOrCreate()` method will check if a record with the given email exists, and if not, it will create a new record with the provided attributes (`['name' => 'New User']`). You can then check the `$user->wasRecentlyCreated` property to determine if the record was just created or if it already existed.

Citations:
[1] https://bobbyiliev.com/post/how-to-check-if-a-record-exists-with-laravel-eloquent/
[2] https://laraveldaily.com/post/dont-check-record-exists-methods-orcreate-ornew
[3] https://laracasts.com/discuss/channels/laravel/how-do-you-properly-check-if-a-record-exists
[4] https://www.itsolutionstuff.com/post/how-to-check-if-record-exists-or-not-in-laravel-5example.html
[5] https://devdojo.com/bobbyiliev/how-to-check-if-a-record-exists-with-laravel-eloquent