Laravel Query Kit
, (*1)
This package provides a handy way to query eloquent-scopes on model instances in Laravel., (*2)
Traditionally you may find yourself having a scopeAccepted and then additionally a ĂŹsAccepted helper method on your model., (*3)
Well, Bon Voyage code-duplication. QueryKit is here to the rescue! đ„, (*4)
--, (*5)
Makeable is web- and mobile app agency located in Aarhus, Denmark., (*6)
Installation
You can install this package via composer:, (*7)
``` bash
composer require makeabledk/laravel-querykit, (*8)
For Laravel version prior 5.5: Add the service provider to your config/app.php:
```php
'providers' => [
...
Makeable\QueryKit\QueryKitServiceProvider::class,
];
Usage
Whenever you have a query scope on an Eloquent Model, you can apply the following trait to add QueryKit:, (*9)
class Job extends Eloquent {
use \Makeable\QueryKit\QueryKit;
public function scopeHired($query)
{
return $query->whereIn('status', ['started', 'finished']);
}
}
Out of the box Laravel offers us a convenient way to query against our database:, (*10)
Job::hired()->first(); // a job with either 'started' or 'finished' status
But with query-kit you are now also able to check if a model instance passes a given scope:, (*11)
$startedJob->passesScope('hired'); // true
$pendingJob->passesScope('hired'); // false
Pretty cool, right?, (*12)
Much more advanced functionality is supported than this simple example., (*13)
See Currently supported methods further down., (*14)
Provided methods on QueryKit
passesScope
/**
* Check if a model passes the given scope
*
* @param $name
* @param array ...$args
* @return bool
*/
public function passesScope($name, ...$args)
failsScope
/**
* Check if a model fails the given scope
*
* @param $name
* @param array ...$args
* @return bool
*/
public function failsScope($name, ...$args)
Currently supported methods
As of this moment QueryKit supports the following query methods, (*15)
- orWhere
- orWhereIn
- orWhereBetween
- orWhereDate
- orWhereDay
- orWhereMonth
- orWhereNotBetween
- orWhereNotIn
- orWhereNotNull
- orWhereNull
- orWhereTime
- orWhereYear
- where
- whereIn
- whereBetween
- whereDate
- whereDay
- whereMonth
- whereNotBetween
- whereNotIn
- whereNotNull
- whereNull
- whereTime
- whereYear
- whereColumn
QueryKit tries to support most of the argument types that Eloquent Builder supports, but there might exceptions., (*16)
Also, do note that advanced joins and relations queries won't work., (*17)
Extending QueryKit
Say that you want to add functionality for Laravel QueryBuilder's 'whereBetween' method:, (*18)
Create a WhereBetween that implements \Makeable\QueryKit\Contracts\QueryConstraint., (*19)
class WhereBetween implements \Makeable\QueryKit\Contracts\QueryConstraint
{
public function __construct(...$args)
{
// Accept scope arguments here
}
public function check($model)
{
// Return boolean
}
}
Next register the constraint in your AppServiceProvider's register method:, (*20)
public function register()
{
\Makeable\QueryKit\Builder\Builder::registerConstraint(WhereBetween::class);
}
You can also use the above method to override the existing implementations., (*21)
Make sure to checkout our makeabledk/laravel-eloquent-status package that streamlines the way you handle model-state across your application., (*22)
Testing
You can run the tests with:, (*23)
composer test
Contributing
We are happy to receive pull requests for additional functionality. Please see CONTRIBUTING for details., (*24)
Credits
License
Attribution-ShareAlike 4.0 International. Please see License File for more information., (*25)