PHP code example of anourvalar / eloquent-request
1. Go to this page and download the library: Download anourvalar/eloquent-request library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anourvalar / eloquent-request example snippets
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'));
}
}
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'));
}
}
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'));
}
}
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'));
}
}
$profile = [
'filter' => [
'id' => ['in'],
],
];
$request = [
'filter' => ['id' => ['in' => [1,2,3]]],
];
$collection = \EloquentRequest::buildBy(\App\User::class, $profile, $request);
\EloquentRequestFlat::sync(\App::make(\App\Drivers\ModelFlat::class), $model);
\EloquentRequestFlat::createTable(\App::make(\App\Drivers\ModelFlat::class));
\EloquentRequestFlat::dropTable(\App::make(\App\Drivers\ModelFlat::class));
'flat' => [
'shadow' => false,
],
if (! \EloquentRequestFlat::isActualTable($flatInterface)) {
\EloquentRequestFlat::createTable($flatInterface);
\EloquentRequestFlat::resync($flatInterface, \App\Model::class);
}
'flat' => [
'shadow' => true, // it's recommended to false when the structure is permanent
],
if (! \EloquentRequestFlat::isActualTable($flatInterface)) {
\EloquentRequestFlat::createTable($flatInterface);
}
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);
});
}