In this laravel eloquent model tutorial we will discuss how to retrive data throw model by get and all method with example.
Laravel data retrive method
- get()
- all()
Laravel data retrive by model using get() method
laravel provides get() model method for retrive data from database table. It retrive data in array form for each row and get method use simply with model and after where conditions. Let an example of laravel get method. Here we define our model Student use App\Models\Student; at top.
Controller code<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $students = Student::get(); OR $students = Student::where('status','1')->get(); return view('student', compact('students')); } }View file student.blade.php
<div> @foreach($students as $student) <p> {{ $student->id }} </p> <p> {{ $student->name }} </p> <p> {{ $student->email }} </p> @endforeach </div>
Laravel model data retrive by using all() method
laravel provide another method all() for retrive model data only difference between laravel get and all method is all method get all model data it not use any type of where conditions. Let an example of laravel all() method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $students = Student::all(); dd($students); } }
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