anourvalar / eloquent-request
Dynamic Query Builder based on request
Installs: 9 006
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- anourvalar/laravel-atom: ^2.17
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.26
- orchestra/testbench: ^8.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- psalm/plugin-laravel: ^2.8
- squizlabs/php_codesniffer: ^3.7
- dev-master
- 2.8.1
- 2.8.0
- 2.7.8
- 2.7.7
- 2.7.6
- 2.7.5
- 2.7.4
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 1.21.3
- 1.21.2
- 1.21.1
- 1.21.0
- 1.20.1
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.4
- 1.17.3
- 1.17.2
- 1.17.1
- 1.17.0
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.4
- 1.11.3
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.6
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.3.0
- 0.2.0
This package is auto-updated.
Last update: 2024-10-10 15:17:44 UTC
README
- Filling up the QueryBuilder from user data. Key feature is safety: all data is under a validation.
- Profile-based approach limit access to the columns and operations.
Installation
composer require anourvalar/eloquent-request
Usage: basic
Request
{ "filter": { "created_at": {">": "2021-01-01"} }, "sort": { "created_at": "DESC" } }
Code
class UserController extends Controller { /** * Profile */ protected $profile = [ 'filter' => [ 'created_at' => ['=', '!=', '<', '<=', '>', '>=', 'in', 'not-in'], ], 'sort' => ['created_at'], ]; /** * Users list */ public function index(Request $request) { $users = \App::make(\AnourValar\EloquentRequest\Service::class)->buildBy( \App\User::class, $this->profile, $request->input() ); // Equals to: // \App\User // ::where('created_at', '>', '2021-01-01') // ->orderBy('created_at', 'DESC') // ->paginate($request->input('page')); } }
Usage: relations & QueryBuilder preconfigure
Request
{ "filter": { "userPhones.phone_number": {"like": "1234"} } }
Code
class UserController extends Controller { /** * Profile */ protected $profile = [ 'filter' => [ 'userPhones.phone_number' => ['like'], ], ]; /** * Users list */ public function index(Request $request) { $users = \App::make(\AnourValar\EloquentRequest\Service::class)->buildBy( \App\User::where('status', '=', 'active'), $this->profile, $request->input() ); // Equals to: // \App\User // ::where('status', '=', 'active') // ->whereHas('userPhones', function ($query) // { // $query->where('phone_number', 'like', '%1234%'); // }) // ->paginate($request->input('page')); } }
Usage: simple pagination
Code
class UserController extends Controller { /** * Profile */ protected $profile = [ 'options' => [ \AnourValar\EloquentRequest\Actions\PaginateAction::OPTION_SIMPLE_PAGINATE, \AnourValar\EloquentRequest\Actions\PaginateAction::OPTION_PAGE_MAX => 20, ], ]; /** * Users list */ public function indexAny() { $list = $this->buildBy( \App\User::whereNotNull('email_verified_at') ); // Equals to: \App\User::whereNotNull('email_verified_at')->simplePaginate($request->input('page')); } }
Usage: advanced features
Code
class UserController extends Controller { use \AnourValar\EloquentRequest\ControllerTrait; // helper for quick usage /** * Profile */ protected $profile = [ 'filter' => [ 'created_at' => \AnourValar\EloquentRequest\Events\RequestBuiltEvent::PROFILE_FILTER_DATE, // preset ], 'ranges' => [ 'created_at' => ['min' => '2018-01-01'], // filter's constrainment ], 'scope' => [ 'customStuff', // Eloquent scope ], 'sort' => ['created_at'], ]; /** * Users list */ public function indexAny() { $users = $this->buildBy(\App\User::where('status', '=', 'active')); } }
Usage: via facade
Code
$profile = [ 'filter' => [ 'id' => ['in'], ], ]; $request = [ 'filter' => ['id' => ['in' => [1,2,3]]], ]; $collection = \EloquentRequest::buildBy(\App\User::class, $profile, $request);
Usage: Flat table
Setup
Model Observer (saved, deleted):
\EloquentRequestFlat::sync(\App::make(\App\Drivers\ModelFlat::class), $model);
Migration (up):
\EloquentRequestFlat::createTable(\App::make(\App\Drivers\ModelFlat::class));
Migration (down):
\EloquentRequestFlat::dropTable(\App::make(\App\Drivers\ModelFlat::class));
"Simple" workflow
Config:
'flat' => [ 'shadow' => false, ],
Seeder:
if (! \EloquentRequestFlat::isActualTable($flatInterface)) { \EloquentRequestFlat::createTable($flatInterface); \EloquentRequestFlat::resync($flatInterface, \App\Model::class); }
"Shadow" workflow
Config:
'flat' => [ 'shadow' => true, // it's recommended to false when the structure is permanent ],
Seeder:
if (! \EloquentRequestFlat::isActualTable($flatInterface)) { \EloquentRequestFlat::createTable($flatInterface); }
After deploy:
if (\EloquentRequestFlat::shadow($flatInterface)) { $closure = function ($flatInterface, $model) { \DB::transaction(function () use ($flatInterface, $model) { // Atomic lock (for sync): // <...> $this->syncSoft($flatInterface, $model->fresh()); }); }; \EloquentRequestFlat::resync($flatInterface, \App\Model::class, $closure); \DB::transaction(function () use ($flatInterface) { // Atomic lock (for sync): // <...> \EloquentRequestFlat::switchShadow($flatInterface); }); }