In this laravel tutorial we will discuss how to create and retrieve data by eloquent model firstOrCreate() and firstOrNew() method and difference between laravel eloquent firstOrCreate and firstOrNew method.
Laravel data create and retrieve method
- firstOrCreate()
- firstOrNew()
Laravel eloquent firstOrCreate method
Laravel firstOrCreate method work as when we retrieve some data by passing column name and value array in the first argument if data match in the database then it retrieves that row data else data not found it create a new row. firstOrCreate method also have an optional second argument which is an array of additional column and value that are save with first argument data if it not match. Let us more explain by example
Example 1 firstOrCreate simple example<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::firstOrCreate([ 'name' => 'anmol sharma' ]); dd($student); } }
In above example retrieve data from students table where name equal to 'anmol sharma' else it create a new row in table with name 'anmol sharma'
Example 2 firstOrCreate example with second argument<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::firstOrCreate( ['name' => 'anmol sharma'], ['email' => 'anmol@gmail.com', 'phone' => '123456789'] ); dd($student); } }
In above example if first argument data match than it retrieve that row else it create new row in table with name, email and phone
Laravel eloquent firstOrNew method
Laravel firstOrNew same as firstOrCreate method retrieve data if find given attribute but in here if data do not match then it creates new model instance which is directly not saved in database it only retrieves new data at that time or if you want to save data then call save() method on that model instance. Thus the only difference between firstOrCreate and firstOrNew method is firstOrCreate retrieve and save data in database and firstOrNew retrieve and save data in only model instance. Let more explain by example
Example 1 firstOrNew simple example<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::firstOrNew([ 'name' => 'advanced web tuts' ]); dd($student); } }
In above example retrieve data from students table where name equal to 'advanced web tuts' else it create a new model instance
Example 2 firstOrNew example with second argument<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::firstOrNew( ['name' => 'advanced web tuts'], ['email' => 'advancedweb@gmail.com', 'phone' => '123456789'] ); dd($student); } }
In above example if first argument data match than it retrieve that row else it create new model instance with name, email and phone
You read this tutorial on advanced web tutorial. here we provide laravel beginners to advanced tutorial.
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- What is laravel eloquent model and introduction
- part 1 laravel eloquent model all configuration hindi
- part 2 laravel insert and create data by eloquent model tutorial
- part 3 laravel data retrieve method
- part 4 first, find, findOrFail and firstOrFail method
- part 5 laravel delete vs truncate method
- part 6 laravel save vs update method
- part 7 laravel updateOrCreate and upsert
- part 8 laravel chunk vs cursor method
- part 9 laravel eloquent subquery select
- part 10 laravel findOrFail vs firstOrFail
- part 11 laravel create and retrieve data
- part 12 laravel eloquent aggregate functions
- part 13 laravel isDirty vs isClean vs wasChanged method