2017 © Pedro PelĂĄez
 

library laravel-querykit

image

makeabledk/laravel-querykit

  • Tuesday, July 3, 2018
  • by makeabledk
  • Repository
  • 2 Watchers
  • 19 Stars
  • 328 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 215 % Grown

The README.md

Laravel Query Kit

Latest Version on Packagist Build Status StyleCI, (*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)

The Versions

03/07 2018

dev-master

9999999-dev

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires

02/07 2018

v1.2.0

1.2.0.0

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires

19/03 2018

v1.1.0

1.1.0.0

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires

19/03 2018

dev-analysis-8PWljW

dev-analysis-8PWljW

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires

05/09 2017

v1.0.1

1.0.1.0

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires

30/06 2017

v1.0.0

1.0.0.0

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires

27/06 2017

v0.9.0

0.9.0.0

  Sources   Download

CC-BY-SA-4.0

The Requires

 

The Development Requires