PHP code example of papalapa / laravel-query-filter
1. Go to this page and download the library: Download papalapa/laravel-query-filter 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/ */
papalapa / laravel-query-filter example snippets
/**
* @property $id int
* @property $firstname string
* @property $lastname string
* @property $role string|null
* @property $created_at Carbon
*/
class User {}
// ...
Route::get('/users', [UserController::class, 'users']);
Route::get('/users/superusers', [UserController::class, 'superusers']);
Route::get('/users/all', [UserController::class, 'all']);
// ...
class UserController {
public function users() {
return User::query()->whereNull('role')->get();
}
public function superusers() {
return User::query()->whereNotNull('role')->get();
}
public function all() {
return User::query()->orderBy('username')->get();
}
}
use Papalapa\Laravel\QueryFilter\BaseDataProvider;
final class UserDataProvider extends BaseDataProvider
{
/**
* Default sorting columns, when other not set
*/
protected array $defaultSort = [
'id' => 'asc',
];
/**
* Final sorting columns, which use always
*/
protected array $finalSort = [
'created_at' => 'desc',
];
/**
* Safe-attributes to use in filtration
*/
protected array $allowedFilter = [
'name' => ['lastname', 'firstname'], // alias of two columns
'role',
];
/**
* Safe-attributes to use in sorting
*/
protected array $allowedSort = [
'name' => ['lastname', 'firstname'], // alias of two columns
'datetime' => 'created_at',
];
protected function makeBuilder() : EloquentBuilder
{
return User::query()
->select([
'id',
'lastname',
'firstname',
'role',
]);
}
}
Route::get('/users', [UserController::class, 'users']);
class UserController {
public function users(UserDataProvider $dataProvider) {
return $dataProvider->paginate();
}
}
<> >= != <= > = <
! === NOT LIKE '%xxx%'
* === LIKE '%xxx%'
^ === LIKE '%xxx'
$ === LIKE 'xxx%'
{"role": null} === role IS NULL
{"role": "~"} === role IS NOT NULL
or
{"is null": "role"}
{"is not null": "role"}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.