PHP code example of phatnt99 / advanced-query
1. Go to this page and download the library: Download phatnt99/advanced-query 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/ */
phatnt99 / advanced-query example snippets
composer
Phatnt99\AdvancedQuery\QueryServiceProvider::class
return $query->where('name','LIKE', '%'.$name.'%');
...
return $query->where('email', '=', $email);
...
return $query->whereDate('created_at', '=', date('Y-m-d'));
protected $filterPartial = [
'name'
];
protected $filterExact = [
'email'
];
protected $filterDate = [
'created_at'
];
protected $filterDate = [
'from.created_at',
'to.created_at'
];
protected function project($projectId)
{
return $this->query->whereHas('projects', function ($query) use ($projectId) {
$query->where('project_id', '=', $projectId);
});
}
protected $defaultSorts = [
'full_name',
'nick_name',
'dob',
'email',
'phone_number',
];
protected $defaultSorts = [
...
'dob' => SortDirection::DESCENDING,
];
/**
* Your model class
* @var string
*/
protected $model = User::class;
/**
* Your filter class
* @var string
*/
protected $filter = UserFilter::class;
/**
* Your sort class
* @var string
*/
protected $sort = UserSort::class;
// UserController
public function index(UserQuery $query)
{
return response()->json(
$query->filter()
->sort()
->paginate());
}
return response()->json(
$query->filter(null, ['id', 'name'])
->sort(null, ['created_at'])
->paginate());
// UserQuery
/**
* Advanced Query
*/
public function verifiedUser() {
$this->query->whereNotNull('email_verify_at');
return $this;
}
$filter = new \App\Queries\Filters\UserFilter();
$filter->setQuery(User::query());
$filter = new \App\Queries\Filters\UserFilter();
$filter->setQuery(User::query())
->setAllowAttrs(['name' => 'John'])
->getCollection();
-Queries
|
--Filters
| |
| --UserFilter.php
| |
--Sorts
| |
| --UserSort.php
|
--UserQuery.php
php artisan make:filter UserFilter
php artisan make:sort UserSort