How to Create and Use Query Scope in Laravel Eloquent

How to Create and Use Query Scope in Laravel Eloquent

In this post we will give you information about How to Create and Use Query Scope in Laravel Eloquent. Hear we will give you detail about How to Create and Use Query Scope in Laravel EloquentAnd how to use it also give you demo for it if it is necessary.

Today, i would like to share example of how to use laravel eloquent model scope. i will show you how to create model eloquent query scope in laravel 6 and how it will easy for you. i will guide you to create custom query function in model eloquent using scope in laravel 6.

You can easily use dynamic query scope in laravel 6 application.

Sometime, we are working model query like get todays records, get active records, get banned users etc. At this time we always use where condition everywhere on controller file. i think where condition is right it is not wrong, but you have to write if again and again with some login like current date and etc. If you create laravel model eloquent scope then you don’t have to write same logic with where condition again and again. You can just use function like “latest()”. You can easily re-use that scope on your model query.

Here i will give you simple with today() scope and it will get only today records. So let’s see this example here:

Create Scope in Model

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

app/Post.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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 IlluminateDatabaseEloquentBuilder $query

* @return IlluminateDatabaseEloquentBuilder

*/

public function scopeToday($query)

{

return $query->whereDate('created_at', CarbonCarbon::today());

}

}

Use Scope Query

Now you can see how you can use that with your controller file.

Post::select("*")->today()->get();

Dynamic Scope in Model

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

app/Post.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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 IlluminateDatabaseEloquentBuilder $query

* @return IlluminateDatabaseEloquentBuilder

*/

public function scopeStatus($query, $type)

{

return $query->where('status', $type);

}

}

Use Scope Query

Now you can see how you can use that with your controller file.

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

I hope it can help you…

Hope this code and post will helped you for implement How to Create and Use Query Scope in Laravel Eloquent. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

5  +  1  =  

We accept paid advance and paid guest Posting on our Site : Contact US