In this tutorial you will learn about the Laravel 8 Query Scope Example and its application with practical example.
In this Laravel 8 Query Scope Example Tutorial I will show you how to use query scopes in laravel eloquent model. In this tutorial you will learn to implement and use query scopes in laravel eloquent model. I will Also show you how to create and use dynamic query scope in laravel applications.
Laravel 8 Query Scope Example
In this step by step guide I will demonstrate you to create and use dynamic query scope in laravel applications.
Create Basic Scope in Model
Here we will first create a basic scope. Go to app/Post.php and create a scope here:
app/Post.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { public $table = "posts"; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'id', 'title', 'body', 'status' ]; /** * Scope a query to only include popular users. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeStatus($query) { return $query->whereDate('status', 1); } } |
Use Basic Query Scope on Controller:
We can use basic query scope as following:
1 |
Post::status()->get(['id','title']); |
Laravel 8 Create Dynamic Scope in Model
Now we are creating a dynamic query scope in laravel model as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { public $table = "posts"; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'id', 'title', 'body', 'status' ]; /** * Scope a query to only include popular users. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeStatus($query, $type) { return $query->where('status', $type); } } |
Dynamic Scope Query On Controller
We can use dynamic scope in laravel as following:
1 |
Post::status(1)->get(['id',title']); |
Laravel 8 Apply Scope with Relationship
Here, you will learn how to use query scopes with laravel relationship. Go to your App/Category.php and create a relationship between Categories and Posts Tables:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Category extends \Eloquent { public function posts() { return $this->HasMany('Post'); } } |
Go to your App/Post.php and create a relationship between Posts and Categories Tables:
1 2 3 4 5 6 7 8 9 10 11 |
class Post extends \Eloquent { public function category() { return $this->belongsTo('Category'); } public function scopePublished($query) { return $query->where('published', 1); } } |
Query scope with relationship
Here we can use query scope with relationship as following:
1 2 3 4 5 |
$categories = Category::with(['posts' => function ($q) { $q->published(); }])->get(); |