PHP code example of bakgul / laravel-query-helper

1. Go to this page and download the library: Download bakgul/laravel-query-helper 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/ */

    

bakgul / laravel-query-helper example snippets


class User extends ...
{
    use IsFilterable, ...;

    public static $filters = [
        'self' => [
            \Bakgul\LaravelQueryHelper\Filters\Name::class,
            \Bakgul\LaravelQueryHelper\Filters\Email::class,
        ],
        'with' => [
            'roles' => Role::class,
        ]
    ];

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

class Role extends Model
{
    use IsFilterable;

    private static array $filters = [
        'self' => [
            \Bakgul\LaravelQueryHelper\Filters\Name::class,
        ],
        'with' => [
            'users' => User::class,
            'abilities' => Ability::class,
        ],
    ];

    public function users()
    {
        return $this->belongsToMany(User::class);
    }

    public function abilities()
    {
        return $this->belongsToMany(Ability::class);
    }
}

class Ability extends Model
{
    use IsFilterable;

    private static array $filters = [
        'self' => [
            \Bakgul\LaravelQueryHelper\Filters\Name::class
        ],
        'with' => [
            'roles' => Role::class,
        ]
    ];

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

$users = User::filter($request->filters)->get();

[
    // *** will be replaced by % by the Text filter.
    // ***x means the string that ends with 'x'
    // x*** means the string that start with 'x',
    // ***x*** means the string that contains 'x',
    // x means the string that is 'x'
    'name' => ['***x***', '***y'],
    'with' => [
        'roles' => [
            'name' => ['editor***'],
            'with' => [
                'abilities' => [
                    'name' => ['delete']
                ]
            ]
        ]
    ]
]

class Post extends Model
{
    use IsFilterable;

    private static array $filters = [
        'self' => [
            \Bakgul\LaravelQueryHelper\Filters\MorphMany::class
        ],
        'with' => [
            'user' => User::class,
        ],
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->morphToMany(Comment::class, 'commentable');
    }
}

[
    'morph_many' => [
        // method name: 'to' if the relation method is 'morphToMany'
        //              'by' if the relation method is 'morphedByMany'
        'to',
        // relationsip name
        'comments',
        // prefix:
        //     if 'to' then this will be 'comment_id'
        //     if 'by' then this will be 'commentable_id' and 'commentable_type' 
        'comment',
        // the N number of ids that will be cheched in the column up above
        2, 3, 5
    ]
]

/**
 * users table has these columns:
 *     first_name, 
 *     last_name, 
 *     email, 
 *     ... some irrelevant columns
 *     created_at
 */

$users = User::group(['first_name']);

$users = User::group(
    keys: ['year', 'email_provider'],
    take: 5,
    isLast: true,
    select: ['first_name', 'last_name', 'email']
    column: 'updated_at'
);

$users = User::modify(
    keys: ['year', 'month'],
    select: ['name', 'email'],
    column: 'updated_at'
)->get();

User::sort(['name'], ['email', 'desc']);

namespace App\Filters;

class City extends Text
{
    public $column = 'city';
}

namespace App\Filters;

class City extends Filter
{
    protected function filter(Builder $query, mixed $filter): Builder
    {
        // if you want to accept multiple values, call filters method
        return $this->filters($query, $filter, $this->callback());

        // otherwise...
        return $query->where('city', $filter);
    }

    protected function callback(): callable
    {
        return fn ($query, $filter) => $query->where('city', $filter);
    }
}

class Address extends Model
{
    private static array $filters = [
        'self' => [
            \Bakgul\LaravelQueryHelper\Filters\Name::class,
            \App\Filters\City::class,
        ],
        'with' => [
            'user' => User::class,
            'country' => Country::class,
        ],
    ];
}

Adress::filter(['city' => ['ankara', 'london']])->get();

User::filter(['with' => ['city' => ['ankara', 'london']]])->get();

namespace App\Modifiers;

class TopLevelDomain extends Modify
{
    public function modifyQuery(Builder $query, array $keys, string $column): Builder
    {
        $raw = $this->rawQuery();

        return $query->when(
            in_array('top_level_domain', $keys),
            fn ($q) => $q->addSelect($raw)
        );
    }

    private function rawQuery(): Expression
    {
        return DB::raw("REPLACE(domain, SUBSTRING_INDEX(domain, '.', 1) , '') as top_level_domain");
    }
}

    'modifiers' => [
        // default ones,
        \App\Modifiers\TopLevelDomain::class,
    ]

$customers = Customer::group(['top_level_domain']);