All Projects → kblais → query-filter

kblais / query-filter

Licence: MIT license
Define filters for your Eloquent models based on your request

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to query-filter

Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (-5%)
Mutual labels:  eloquent, laravel-package
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (+290%)
Mutual labels:  eloquent, laravel-package
LaraPersonate
Login as a different user quickly
Stars: ✭ 121 (+505%)
Mutual labels:  eloquent, laravel-package
Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (+295%)
Mutual labels:  eloquent, laravel-package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+890%)
Mutual labels:  eloquent, laravel-package
Laravel-Auto-Hard-Deleter
Laravel and Lumen Auto Hard Deleter
Stars: ✭ 34 (+70%)
Mutual labels:  eloquent, laravel-package
Eloquent Ldap
A Laravel 5.1 package that first tries to log the user against the internal database if that fails, it tries against the configured LDAP/AD server.
Stars: ✭ 19 (-5%)
Mutual labels:  eloquent, laravel-package
Eloquent Relativity
Allows you to decouple your eloquent models from one another.
Stars: ✭ 112 (+460%)
Mutual labels:  eloquent, laravel-package
Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (+585%)
Mutual labels:  eloquent, laravel-package
laravel-sybase
Connection and Laravel Eloquent driver for Sybase
Stars: ✭ 29 (+45%)
Mutual labels:  eloquent, laravel-package
eloquent-repository
Repository pattern for Eloquent ORM with focus in cache.
Stars: ✭ 30 (+50%)
Mutual labels:  eloquent, laravel-package
laravel-package
Laravel package template
Stars: ✭ 31 (+55%)
Mutual labels:  laravel-package
laravel-login-links
Create (passwordless) login links for users
Stars: ✭ 13 (-35%)
Mutual labels:  laravel-package
artisan-remote
Artisan Remote is a package for Laravel to interact with your Artisan Commands via an HTTP API.
Stars: ✭ 69 (+245%)
Mutual labels:  laravel-package
menus
Laravel Enso main menu manager, for easy management of the application menus straight from the interface, whether that means adding, removing or reordering them
Stars: ✭ 15 (-25%)
Mutual labels:  laravel-package
qqmap-region
腾讯位置服务中国标准行政区划数据 SDK
Stars: ✭ 22 (+10%)
Mutual labels:  laravel-package
Laravel-Crud-Generator
A Simple Laravel Library that allows to create crud operation with a single command
Stars: ✭ 20 (+0%)
Mutual labels:  laravel-package
laravel-sti
Single table inheritance with Laravel/Eloquent
Stars: ✭ 16 (-20%)
Mutual labels:  eloquent
laravel-hasmanywithinverse
Define HasMany while also setting the inverse relationship in Laravel.
Stars: ✭ 57 (+185%)
Mutual labels:  eloquent
laravel-getid3
A Laravel package to extract metadata from media files. mp3, aac, etc. It can be used with files stored on different disks such as local disk, S3 etc.
Stars: ✭ 50 (+150%)
Mutual labels:  laravel-package

QueryFilter Build Status

Easily create filters for your Eloquent model.

Based on Jeffray Way's Laracast tutorial.

Installation

You can install the package via composer:

composer require kblais/query-filter

You can publish the config file with:

php artisan vendor:publish --provider="Kblais\QueryFilter\QueryFilterServiceProvider" --tag="query-filter-config"

This is the contents of the published config file:

return [
    'default-filters-source' => null,
];

Usage

A QueryFilter is a class to apply, based on an array or a Request, multiple conditions.

You can call any Eloquent method directly from filter methods.

use Kblais\QueryFilter\QueryFilter;

class PostFilter extends QueryFilter
{
    public function title($value)
    {
        return $this->where('foo', 'bar');
    }

    public function author($value)
    {
        return $this->whereHas('author', function ($builder) use ($value) {
            $this->where('name', $value);
        });
    }
}

To allow a model to use query filters, you have to add the Filterable trait on your model.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kblais\QueryFilter\Filterable;

class Post extends Model
{
    use Filterable;
}

You can then use the filter() scope from anywhere:

// From an array...
$filterInput = [
    'title' => 'Les Trois Mousquetaires',
];

$posts = Post::filter(PostFilter::make($filterInput))->get();

// ...Or in a controller action
public function index(PostFilter $filter)
{
    // Filter is automatically populated with Request data when injected
    return Post::filter($filter)->get();
}

If your filter parameters are always placed in an array key (for example filters), you can define the default-filters-source config key in the config file, or add a protected string $source = 'filters' in your QueryFilter.

Frequent issues

Call to an undefined method App\QueryFilters\YourFilter::anEloquentScope(). with PHPStan

To fix this error message, add the following DocBlock to your filter:

/**
 * @mixin \App\Models\ModelOfYourScope
 */

Testing

The test suite is composed of 3 tests: PHPCsFixer (coding style), PHPStan (static analysis) and PHPUnit (unit tests).

You can run all these tests running the following command:

composer tests

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

  • Follow the PSR-2 Coding Standard. Use PHP-CS-Fixer to apply the conventions.
  • Add tests for the features you add and bugs you discover.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].