Laravel Query Scope Example

Laravel query scope example; Through this tutorial, i am going to show you how to use query scope with relationship, join and select query in laravel apps.

Laravel Query Scope Example

Use the following steps for how to use query scope with relationship, join and select query in laravel apps:

  • Create Basic Scope in Model
  • Create Dynamic Scope in Model
  • Use Query Scope with Relationship

Create Basic Scope in Model

Let’s see the following example to create basic query scope in laravel model; is as follows:

<?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);
    }
}

And you can use query scope in laravel controller; is as follows:

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

Create Dynamic Scope in Model

Let’s see the following example to create dynamic query scope in laravel model; is as follows:

<?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);
    }
}

And you can use dynamic query scope in laravel controller; is as follows:

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

Use Query Scope with Relationship

Let’s see the following example to create dynamic query scope with relationship in laravel model; is as follows:

class Category extends \Eloquent

{

    public function posts()

    {

        return $this->HasMany('Post');

    }

}

And create a relationship between Posts and Categories Tables; is as follows:

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

And you can use dynamic query scope with relationship in laravel controller; is as follows:

$categories = Category::with(['posts' => function ($q) {

  $q->published();

}])->get();

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.