Download the PHP package mustafaomar/laravel-qsw without Composer
On this page you can find all versions of the php package mustafaomar/laravel-qsw. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mustafaomar/laravel-qsw
More information about mustafaomar/laravel-qsw
Files in mustafaomar/laravel-qsw
Package laravel-qsw
Short Description Cleanly apply filters on a query builder depending on query string parameters.
License MIT
Informations about the package laravel-qsw
Laravel query-string watcher
Cleanly apply filters on a query builder depending on query string parameters.
Sometimes you may want to apply additional logic on the query builder eg: loading some relationships so you can return that relations with the response depending on the query string parameters, or you may want to apply complex filters eg: status=active&with=comments,replies&from=2000-01-10&to=2022-05-25&sort=newest
etc..
This package makes it easy to accomplish this with a great manner.
The main goal of using this package is the separation of concerns, we don't want tons of lines in our controllers.
Installation
You can install it via composer
Usage
There are serveral ways to start using laravel-qsw
Scopable trait
You can use Scopable
in your model and we're done, you now have access to the watch
scope, example.
Article.php
The last step we need is to create a scope, you can do so by using the following command.
For convention please use the model name followed by the query parameter you want to watch for, followed by Scope, for example ?status=success
will be ArticleStatusScope
.
This command will create a scope class similar to this:
Real-life use cases
Now let's create an example describes how to filter data with real-life example.
ArticleController.php
In this scope, we're gonna filter records which are between a date range.
Advanced
If for some reasons you don't want to use the watch
keyword as a scope, you can create your own local scope
.
in the following example we'll create a scope called scopes
.
Article.php
Please don't get confused with scope and Scopes, scope
is a word that tells Laravel we want to use the suffixed word (Scopes
) to call when building a query with the query builder.