class SomeModel extends Model
{
use LGrevelink\CustomQueryBuilder\Concerns\HasCustomQueryBuilder;
protected $queryBuilder = App\QueryBuilders\MyQueryBuilder::class;
}
$builder = SomeModel::select();
// Using direct filters on the query builder
$builder->filterOnProperty(1234);
$builder->filterOnCategories([1, 2]);
$builder->sortByCategory('asc');
$builder->sortByTitle('asc');
// Enforcing the naming structure
$builder->applyFilter('property', 1234); // Calls filterOnProperty
$builder->applyFilter('category', [1, 2]); // Calls filterOnCategories
$builder->applySorting('category', 'asc'); // Calls sortByCategory
$builder->applySorting('title', 'asc'); // Calls sortByTitle
// Setting them in bulk
$builder->applyFilters([
'property' => 1234,
'category' => [1, 2],
); // Calls both filterOnProperty and filterOnCategories
$builder->applySorting([
'category' => 'asc',
'title' => 'asc',
]); // Calls both sortByCategory and sortByTitle
class ProductQueryBuilder extends CustomQueryBuilder
{
public function joinCategories() {
return $this->joinOnce('categories', 'categories.id', 'products.category_id');
}
public function filterOnCategoryStatus(string $status) {
return $this->joinCategories()->where('categories.status', $status);
}
public function filterOnCategoryTitle(string $title) {
return $this->joinCategories()->where('categories.title', 'LIKE', $title);
}
}