For getting every new tutorial link Please join our telegram group

Part 11 how to create and retrieve data by laravel eloquent firstOrCreate() and firstOrNew() method

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

  1. firstOrCreate()
  2. 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.

php laravel developer anmol sharma

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