Laravel 8 Eloquent firstOrCreate() Example

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-firstorcreate-example

In this post, I will explain what is the usage of Laravel Eloquent firstOrCreate() and it…


This content originally appeared on DEV Community and was authored by codeanddeploy

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-firstorcreate-example

In this post, I will explain what is the usage of Laravel Eloquent firstOrCreate() and its importance. Laravel provides firstOrCreate() to help us to attempt to find a record in our database if not found then create a new record and return it.

Example without Laravel firstOrCreate()

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $title = 'Post 3';
        $post = Post::where('title', $title)->first();

        if (is_null($post)) {
            $post = new Post(['title' => $title]);
        }

        $post->description = 'Description for post 3.';
        $post->body = 'Body for post 3.';

        $post->save();

        print_r($post); die;
    }
}


Example with Laravel firstOrCreate()
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $post = Post::firstOrCreate(
            ['title' => 'Post 5'],
            ['description' => 'Description for post 5.', 'body' => 'Body for post 5.']
        );

        print_r($post); die;
    }
}

As you can see of the above codes have the same functionality but using the firstOrCreate() method in Laravel will shorten our code.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-firstorcreate-example if you want to download this code.

Happy coding :)


This content originally appeared on DEV Community and was authored by codeanddeploy


Print Share Comment Cite Upload Translate Updates
APA

codeanddeploy | Sciencx (2022-03-29T07:56:09+00:00) Laravel 8 Eloquent firstOrCreate() Example. Retrieved from https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/

MLA
" » Laravel 8 Eloquent firstOrCreate() Example." codeanddeploy | Sciencx - Tuesday March 29, 2022, https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/
HARVARD
codeanddeploy | Sciencx Tuesday March 29, 2022 » Laravel 8 Eloquent firstOrCreate() Example., viewed ,<https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/>
VANCOUVER
codeanddeploy | Sciencx - » Laravel 8 Eloquent firstOrCreate() Example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/
CHICAGO
" » Laravel 8 Eloquent firstOrCreate() Example." codeanddeploy | Sciencx - Accessed . https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/
IEEE
" » Laravel 8 Eloquent firstOrCreate() Example." codeanddeploy | Sciencx [Online]. Available: https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/. [Accessed: ]
rf:citation
» Laravel 8 Eloquent firstOrCreate() Example | codeanddeploy | Sciencx | https://www.scien.cx/2022/03/29/laravel-8-eloquent-firstorcreate-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.