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']);