churakovmike / laravel-extended-builder
Laravel extended query builder
1.0
2020-04-22 15:39 UTC
Requires
- illuminate/console: ^5.5|^6|^7
- illuminate/contracts: ^5.5|^6|^7
- illuminate/database: ^5.5|^6|^7
- illuminate/http: ^5.5|^6|^7
- illuminate/pagination: ^5.5|^6|^7
- illuminate/support: ^5.5|^6|^7
- illuminate/view: ^5.5|^6|^7
This package is auto-updated.
Last update: 2024-11-23 02:01:48 UTC
README
This package is a good way to organize subqueries of your models.
Requirements
- laravel 5.5+
Getting started
Install
The package is available on packagist.
composer require churakovmike/laravel-extended-builder
Usage
- You need to create a class that will be inherited from ChurakovMike\ExtendedBuilder\ExtendedQuery as an example
<?php namespace App; use ChurakovMike\ExtendedBuilder\ExtendedQuery; /** * Class UserQuery * @package App * * @property string $modelClass */ class UserQuery extends ExtendedQuery { public function isActive() { return $this->where('status', true); } public function hasName($name) { return $this->where('name', $name); } }
- Then you need to add a method to the main model that will call query builder.
<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; /** * Class User * @package App */ class User extends Authenticatable { public static function where() { return new \App\UserQuery(get_called_class()); } }
- Now you can call subqueries from extended query, as well as use regular Bulder methods, see an example
$user = User::where() ->isActive() ->first();
Function call chain example:
$user = User::where() ->isActive() ->hasName('Mike') ->first();