Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dinhquochan/laravel-query-filters
Query Filters in Laravel
https://github.com/dinhquochan/laravel-query-filters
Last synced: about 1 month ago
JSON representation
Query Filters in Laravel
- Host: GitHub
- URL: https://github.com/dinhquochan/laravel-query-filters
- Owner: dinhquochan
- License: mit
- Created: 2019-05-30T17:02:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-14T02:53:54.000Z (about 3 years ago)
- Last Synced: 2024-10-14T10:46:12.515Z (3 months ago)
- Language: PHP
- Size: 17.6 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Query Filters
[![Latest Version on Packagist](https://img.shields.io/packagist/v/dinhquochan/laravel-query-filters.svg?style=flat-square)](https://packagist.org/packages/dinhquochan/laravel-query-filters)
[![Build Status](https://img.shields.io/travis/dinhquochan/laravel-query-filters/master.svg?style=flat-square)](https://travis-ci.org/dinhquochan/laravel-query-filters)
[![Total Downloads](https://img.shields.io/packagist/dt/dinhquochan/laravel-query-filters.svg?style=flat-square)](https://packagist.org/packages/dinhquochan/laravel-query-filters)Laravel Query Filters for [Laravel](https://laravel.com/).
## Requirements
- PHP >= 7.4, >= 8.0
- Laravel >= 6.0## Installation
You can install the package via composer:
```bash
composer require dinhquochan/laravel-query-filters
```If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php
```php
\DinhQuocHan\QueryFilters\QueryFilterServiceProvider::class,
```
## Basic usageCreate basic filter `app/Filters/PostFilter.php`:
```php
getQuery()->where('user_id', $id);
}
}
```In `App\Http\Controllers\PostController`:
```php
of(Post::class)->get();
// or $filter->of(Post::query())->get();
// or $filter->of(new Post())->get();// Send it to view.
return view('posts.index', compact('posts'));
}
}
```### Making a new filter
The package included an artisan command to create a new filter.
```bash
php artisan make:filter PostFilter
```This filter will have the `App\Http\Filters` namespace and will be saved in `app/Http/Filters`.
or into a custom namespace, say, `App\Blog`
```bash
php artisan make:filter "Blog/PostFilter"
```This filter will have the `App\Blog` namespace and will be saved in `app/Blog`.
## Available traits
### Sortable
Allow to sort items, you must add `$sortable` property, default if not call `sort` and `sort_by` in request, the trait will add default sorting column to query:
```php
your-url?sort_by=id
> SELECT * FROM `posts` ORDER BY `id` ASC> your-url?sort_by=id&sort=desc
> SELECT * FROM `posts` ORDER BY `id` DESC
```### Searchable
Allow to search items, you must add `$searchable` property:
```php
your-url?search=foo or your-url?q=foo
> SELECT * FROM `posts` WHERE (`id` LIKE '%foo%' OR `title` LIKE '%foo%')> your-url?search=foo*
> SELECT * FROM `posts` WHERE (`id` LIKE 'foo%' OR `title` LIKE 'foo%')> your-url?search=*foo
> SELECT * FROM `posts` WHERE (`id` LIKE '%foo' OR `title` LIKE '%foo')// your-url?search=foo&search_by=title
// SELECT * FROM `posts` WHERE `title` LIKE '%foo%'
```
### Testing```bash
composer test
```### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
### Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
## Credits
- [Dinh Quoc Han](https://github.com/dinhquochan)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.