Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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 usage

Create 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.