Laravel 8 Eloquent Local Scope Tutorial Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Laravel 8 Eloquent Local Scope Tutorial Example

Hello Dev’s, Today now in this blog, I will share with you a example of how to use the local scope in laravel 8 application. Here I will show with you a example of how to create a model eloquent query scope in laravel application. So now we will simply use to local scope in our laravel application.

And also we can easily use dynamic query scope in our laravel application.

Now here we are working at model query as like as get todays records,or get active records, or get banned users by admin etc. So If we create laravel model eloquent scope then we don’t have to write same logic with where in the condition again and again. Now here i will give you a short and very simple example with today() scope and also it will be get only today records. So let’s start and see this example here:

Create Local Scope in Model

Now we will add today scope in our post model. Then so when we will query in controller then we will use that scope in laravel model.

app/Post.php

<?php
  
namespace App;
  
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 scopeToday($query)
    {
        return $query->whereDate('created_at', \Carbon\Carbon::today());
    }
}
Use Local Scope Query

Now we can see how we can use that with our controller file.

Post::select("*")->today()->get();
Dynamic Local Scope in Model

Here, we will add today scope in our post model. Then So when we query in our controller then we will use that scope in laravel model.

app/Post.php

<?php
  
namespace App;
  
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);
    }
}
Use Local Scope Query

Now here we can see how we can use that with our controller file.

Post::select("*")->status(1)->get();

Read also : How To Set Bcc And Cc Mail Address In Laravel Mail?

I hope it will help you. Also you can follow us on Facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →