This laravel eloquent model tutorial about how to get single row data by fisrt, find and findOrFail method with example.
Laravel first vs find vs findOrFail vs firstOrFail method
- first()
- find()
- findOrFail()
- firstOrFail()
Laravel first() method
laravel provides first() method for getting a single row record. first() method use after a where condition and getting record that are match first. Example of first() method
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::where('age' > '21')->first(); dd($student); } }
Laravel find() method
laravel find() method also retrieve a single row record but it get record which primary key given in find($id) method. it not use where condition. Example of find() method
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $id = 7; $student = Student::find($id); dd($student); } }
Laravel findOrFail() method
laravel findOrFail retrieve single row same as find() method it takes a primary key value in findOrFail($id) method and if any record exist on this id return data else return not found page. Example of findOrFail method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $id = 7; $student = Student::findOrFail($id); dd($student); } }
Laravel firstOrFail() method
laravel firstOrFail method retrieve single row same as the first method it uses where condition and give first match data but if according to where condition no data match than it gives not found page. For example firstOrFail method if no one student find less than age 15 then it gives not found page.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $id = 7; $student = Student::where('age','<', '15')->firstOrFail()); dd($student); } }
You read this tutorial on advanced web tutorial. here we provide beginners to advanced laravel 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