This package is abandoned and no longer maintained. Consider using the spatie/laravel-query-builder package instead.
This package allows you to filter eloquent model queries based on HTTP request.
You can install the package via composer:
composer require stephenjude/simple-query-filter
use Stephenjude\SimpleQueryFilter\WithQueryFilter;
class Post extends Model
{
use WithQueryFilter;
}
The filter()
method is used to filter the rows in a table. The result will only include rows that meets all the criteria of the query parameters.
class PostController extends Controller
{
public function index(Request $request)
{
// GET /posts?title=simple&slug=simple-query-filter
$posts = Post::filter($request->query())->latest()->paginate();
}
}
The scout()
method is used to perform a full search on the model. The result for this method includes any row that meets the search criteria.
class PostController extends Controller
{
public function index(Request $request)
{
// GET /posts?title=simple&slug=simple-query-filter
$posts = Post::scout($request->query())->latest()->paginate();
}
}
You can alternatively pass an array of column names and search strings as a key-value pair to the filter method:
$queryParams = [
'title' => 'Simple',
'description' => 'Query Filter'
];
$posts = Post::filter($queryParam)->latest()->paginate();
$posts = Post::scout($queryParam)->latest()->paginate();
The eloquent filter scope provided in this package will throw a bad request HTTP exception if it fails to find any of the specified column names.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.