PHP code example of alex-storozhenko / eloquent-criteria
1. Go to this page and download the library: Download alex-storozhenko/eloquent-criteria 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/ */
alex-storozhenko / eloquent-criteria example snippets
return [
/*
|--------------------------------------------------------------------------
| Macro
|--------------------------------------------------------------------------
|
| This option controls the ability to add a macro to the eloquent builder,
| which allows, when enabled, to add criteria builder functionality to the eloquent
| at the global level
| methods criteriaQuery(), apply() will be added
| to the Eloquent through the macro
|
*/
'macro_enabled' => env('ELOQUENT_CRITERIA_MACRO_ENABLED', false),
];
ELOQUENT_CRITERIA_MACRO_ENABLED=true
declare(strict_types=1);
namespace App\Criteria\User;
use AlexStorozhenko\EloquentCriteria\Contracts\Criteria;
use Illuminate\Database\Eloquent\Builder;
class BannedUser implements Criteria
{
public function apply(Builder $builder): Builder
{
return $builder->whereNotNull('banned_at');
}
}
declare(strict_types=1);
...
use App\Criteria\User\BannedUser;
...
User::criteriaQuery()->apply(new BannedUser())->paginate();
// or
User::apply(new BannedUser())->paginate();
declare(strict_types=1);
namespace App\Models;
use AlexStorozhenko\EloquentCriteria\Concerns\CriteriaQuery;
use Illuminate\Foundation\Auth\User as Authenticatable;
use AlexStorozhenko\EloquentCriteria\Concerns\CriteriaQuery;
class User extends Authenticatable
{
use CriteriaQuery;
}
declare(strict_types=1);
...
use App\Criteria\User\BannedUser;
...
User::criteriaQuery()->apply(new BannedUser())->paginate();
declare(strict_types=1);
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use AlexStorozhenko\EloquentCriteria\Model as CriteriaModel;
class User extends CriteriaModel implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}
declare(strict_types=1);
...
use App\Criteria\User\BannedUser;
use AlexStorozhenko\EloquentCriteria\CriteriaBuilder;
...
CriteriaBuilder::for(User::query())->apply(new BannedUser())->paginate();
declare(strict_types=1);
...
use App\Criteria\User\BannedUser;
use AlexStorozhenko\EloquentCriteria\CriteriaBuilder;
use AlexStorozhenko\EloquentCriteria\Support\CriteriaChain;
use AlexStorozhenko\EloquentCriteria\Contracts\Criteria;
...
$haveNotLastName = new class() implements Criteria
{
public function apply(Builder $builder): Builder
{
return $builder->whereNull('last_name');
}
};
CriteriaBuilder::for(User::query())->apply(new CriteraChain(new BannedUser(), $hasNotLastName))->paginate();
declare(strict_types=1);
namespace App\Criteria\Common;
use AlexStorozhenko\EloquentCriteria\Contracts\Criteria;
use Illuminate\Database\Eloquent\Builder;
use App\Criteria\Concern\HttpFilters;
use App\DataObject\Filter;
use Illuminate\Http\Request;
class ByRequestFiltersCriteria implements Criteria
{
use HttpFilters; // Dirty job of parsing request query
public function __construct(private readonly Request $request) {}
public function apply(Builder $builder): Builder
{
/** @var Filter $filter */
foreach ($this->getFilters() as $filter) {
$builder->{$filter->getMethod()}(...$filter->getMethodArguments());
}
return $builder;
}
}