Thursday, August 28, 2025
HomeLanguagesLaravel 8 Query Scope Example

Laravel 8 Query Scope Example

Laravel 8 model , relationship, join, select query scope tutorial. In this tutorial, you will learn how to create and use laravel eloquent model query scopes. Also learn how to create and use dynamic query scope in laravel 8 app.

When you work with small and large laravel web application. At that time you need to get/fetch active records from the database tables using the laravel query scopes.

Laravel query scopes to save your time to write code again and again. You can easily reuse laravel query scopes.

Laravel 8 Model Query Scopes

Create Basic Scope in Model

Now, you will learn how to create and use scope in your laravel model. You could do like:

Go to app/Post.php and create a scope here:

<?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:

You could use model query scope in your laravel web application controllers like:

Post::status()->get(['id','title']);

Laravel 8 Create Dynamic Scope in Model

Next, You will learn how to create and use dynamic query scope in laravel web application.

You can create dynamic query scopes in laravel model:

<?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

You could use dynamic model query scope in your laravel web application controllers like:

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:

class Category extends \Eloquent
{
public function posts()
{
return $this->HasMany('Post');
}
}

Visit App/Models/Post.php and create a relationship between Posts and Categories Tables:

class Post extends \Eloquent
 {
    public function category()
    {
        return $this->belongsTo('Category');
    }
 public function scopePublished($query)
    {
        return $query->where('published', 1);
    }
 }

Query scope with relationship

$categories = Category::with(['posts' => function ($q) {
$q->published();
}])->get();

Recommended Laravel Posts

RELATED ARTICLES

Most Popular

Dominic
32244 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6615 POSTS0 COMMENTS
Nicole Veronica
11787 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11831 POSTS0 COMMENTS
Shaida Kate Naidoo
6727 POSTS0 COMMENTS
Ted Musemwa
7008 POSTS0 COMMENTS
Thapelo Manthata
6684 POSTS0 COMMENTS
Umr Jansen
6697 POSTS0 COMMENTS