Skip to content

Instantly share code, notes, and snippets.

@sanampatel
Last active November 8, 2022 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanampatel/8ccabf42eb894ed6249005bbc3474d40 to your computer and use it in GitHub Desktop.
Save sanampatel/8ccabf42eb894ed6249005bbc3474d40 to your computer and use it in GitHub Desktop.
QueryModel is the PHP trait for Laravel-query-builder package by spatie
<?php
namespace App\Traits;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
trait QueryModel
{
// Get Fillable Fields
private function fields()
{
if ($this->getFillable() != NULL) {
return $this->getFillable();
}
return [];
}
// Regular Search Filters
private function getSearchable()
{
if (isset($this->searchable)) {
return $this->searchable;
}
return $this->fields();
}
// Exact Search Filters
private function getExactSearch()
{
$filters = [];
if (isset($this->exactsearch)) {
foreach ($this->exactsearch as $exactsearch) {
array_push($filters, AllowedFilter::exact($exactsearch));
}
}
return $filters;
}
// Scope Search Filters
private function getScopeSearch()
{
$filters = [];
if (isset($this->scopefilter)) {
foreach ($this->scopefilter as $scopefilter) {
array_push($filters, AllowedFilter::scope($scopefilter));
}
}
return $filters;
}
// Merge All Filters
private function getFilterable()
{
return array_merge($this->getSearchable(), $this->getExactSearch(), $this->getScopeSearch());
}
// Get Fields which can be sorted
private function getSortable()
{
if (isset($this->sortable)) {
return $this->sortable;
}
return $this->fields();
}
// Relationships which are allowed to included
private function getIncludes()
{
if (isset($this->includes)) {
return $this->includes;
}
return [];
}
// Things to append into requests
private function getAppends()
{
if (isset($this->appends)) {
return $this->appends;
}
return [];
}
// Combine all in one
public function getQB()
{
$query = QueryBuilder::for(self::class);
// TODO: Check for the valid values
$filters = $this->getFilterable();
if (request()->has('trashed')) {
$filters = array_merge($filters, AllowedFilter::trashed());
}
$query = $query->allowedFilters($filters);
if (request()->has('sort')) {
$query = $query->allowedSorts($this->getSortable());
}
if (request()->has('include')) {
$query = $query->allowedIncludes($this->getIncludes());
}
if (request()->has('append')) {
$query = $query->allowedAppends($this->getAppends());
}
$query = $query->defaultSort('-created_at')
->paginate(request()->per_page ?: config('site.per_page'))
->withQueryString();
return $query;
}
/*
Define this in model
use HasFactory, SoftDeletes, QueryModel;
protected $searchable = [];
protected $exactsearch = [];
protected $scopefilter = [];
protected $sortable = [];
protected $includes = [];
potected $appends = [];
protected $with = []; // Either use $includes or $with
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment