PHP code example of obrainwave / laravel-query-filters
1. Go to this page and download the library: Download obrainwave/laravel-query-filters 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/ */
obrainwave / laravel-query-filters example snippets
use Illuminate\Database\Eloquent\Model;
use Obrainwave\LaravelQueryFilters\Filterable;
class User extends Model
{
use Filterable;
// Optional: define allowed columns in the model
// protected $allowedFilters = ['name', 'email'];
}
use App\Models\User;
$users = User::filter([
'status' => 'active',
'role' => 'admin',
'q' => 'john', // Global search
])->get(); // or ->first()
protected array $allowedFilters = ['status', 'name', 'role'];
public function getAllowedFilters(): array
{
return $this->allowedFilters;
}
public array $allowedFilters = ['status', 'name', 'role'];
$users = User::filter([
'status' => 'active',
'role' => 'admin',
'q' => 'john', // Global search
])->paginate(); // or simplePaginate() | Supports per_page from request or config
use App\Models\User;
$users = User::filter(request())->paginate(); // Supports per_page from request or config
namespace App\Filters;
use Obrainwave\LaravelQueryFilters\QueryFilter;
class UserFilter extends QueryFilter
{
public function role($value)
{
$this->builder->where('role', $value); //You can alter this to
return $this;
}
public function status($value)
{
$this->builder->where('status', $value);
return $this;
}
}