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.
#example-without-laravel-firstorcreate Example without Laravel firstOrCreate() 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; }} 'Post 5'], ['description' => 'Description for post 5.', 'body' => 'Body for post 5.']); print_r($post); die; }}
Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-firstorcreate-example if you want to download this code.