Using Query Scopes in Laravel?

Today i would like to write about Laravel Eloquent’s Query Scopes , Query scopes are allow us to re-use query logic by allowing us to encapsulate database logic inside model class methods.

For example if you have a model called Posts.php, and if you have 3 type of post status fields called published,private,draft.

In general if you what to get all published posts you will write something like show below


How to pass parameters to scope

To pass parameters to scopes , just we need to pass second parameter to the scope method as shown below

That’s it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments Protection Status
Would love your thoughts, please comment.x