Download the PHP package hamidreza-mozhdeh/filter-by-query-string without Composer
On this page you can find all versions of the php package hamidreza-mozhdeh/filter-by-query-string. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hamidreza-mozhdeh/filter-by-query-string
More information about hamidreza-mozhdeh/filter-by-query-string
Files in hamidreza-mozhdeh/filter-by-query-string
Package filter-by-query-string
Short Description Filter Laravel Eloquent results by URL query strings and Eloqunet's scopes.
License
Informations about the package filter-by-query-string
Make Laravel Eloquent Filterable by URL query strings
Please give me a star if you like it.
About Filter by query string
With this package, you can simply use Eloquent scopes to filter the model result by using URL query strings. The advantage of this repository is that you can reuse your local scopes in your business logic (Controllers, Services, etc).
- It's safe
- Reusable local scopes
- Easy to implement
- Customizable
How to install
Run composer require hamidreza-mozhdeh/filter-by-query-string
. It will add a FilterByQueryString
trait to your project.
How to use
-
Add the trait to your models
use FilterByQueryString;
.Category Model:
- In your controller simply pass the Form Request to the Model:
- Example:
$categories = Category::filter($request);
- Please note you have to type hint the
Request
class. -
Important: First of all is better to have a From Request for each of your actions or methods (optional). If not, you have to change
requestMethod
toinput
which is not recommended.CategoryController:
- Example:
CategoryRequest:
-
Define your scopes to use the filters
- It's better to have a directory and traits for your models in some where like
App\Models\Traits\Scopes\CategoryScopesTrait
.CategoryScopesTrait:
- It's better to have a directory and traits for your models in some where like
- To filter the categories' names for example
ossw
use:
http://0.0.0.0/api/categories?name=ossw
- To filter with dates use:
Indexed array:http://0.0.0.0/api/categories?date-between[]=2023-11-23&date-between[]=2023-11-25
Associative array:http://0.0.0.0/api/categories?date-between[from]=2023-11-23&date-between[to]=2023-11-25
Customization
With prefix: http://0.0.0.0/api/categories?filters[name]=ossw