1. Go to this page and download the library: Download ambengers/query-filter 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/ */
ambengers / query-filter example snippets
php artisan make:query-filter PostFilter
use Ambengers\QueryFilter\AbstractQueryFilter;
class PostFilter extends AbstractQueryFilter
{
/**
* Filter the post to get the published ones
*
* @return Illuminate\Database\Eloquent\Builder
*/
public function published()
{
return $this->builder->whereNotNull('published_at');
}
}
use App\Filters\PostFilter;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(PostFilter $filters)
{
$posts = Post::filter($filters);
return PostResource::collection($posts);
}
}
use Ambengers\QueryFilter\AbstractQueryFilter;
class PostFilter extends AbstractQueryFilter
{
/**
* List of filters.
*
* @var array
*/
protected $filters = [
'published' => \App\Filters\Published::class,
];
}
php artisan make:query-filter-object Published
use Illuminate\Database\Eloquent\Builder;
class Published
{
/**
* Handle the filtering
*
* @param Illuminate\Database\Eloquent\Builder $builder
* @param string|null $value
* @return Illuminate\Database\Eloquent\Builder
*/
public function __invokable(Builder $builder, $value = null)
{
$builder->whereNotNull('published_at');
}
}
/** Sorting */
/posts?sort=created_at|desc
/** Pagination */
/posts?page=2
/** Pagination */
/posts?page=2&per_page=10
class PostFilter extends AbstractQueryFilter
{
/**
* List of searchable columns
*
* @var array
*/
protected $searchableColumns = ['subject', 'body'];
}
class PostFilter extends AbstractQueryFilter
{
/**
* List of searchable columns
*
* @var array
*/
protected $searchableColumns = [
'subject',
'body',
'comments' => ['body'],
];
}
php artisan make:query-loader PostLoader
use App\Loaders\PostLoader;
class PostFilter extends AbstractQueryFilter
{
/**
* Loader class
*
* @var string
*/
protected $loader = PostLoader::class;
}
class PostLoader extends AbstractQueryLoader
{
/**
* Relationships that can be lazy/eager loaded
*
* @var array
*/
protected $loadables = [
'comments', 'author'
];
}
/posts?load=comments,author
class PostController extends Controller
{
/**
* Display the specified resource.
*
* @param App\Models\Post $post
* @param App\Loaders\PostLoader $loader
* @return Illuminate\Http\JsonResponse
*/
public function show(Post $post, PostLoader $loader)
{
$post = $post->filter($loader);
return response()->json($post);
}
}
/posts/1?load=comments,author
/posts/1?load=comments|withTrashed // comments will
return [
// The method to call to use the query filter
'method' => 'fooBar', // Now call $post->fooBar($loaders)
...
]
public function render () {
$filters = app(PostFilter::class)->parameters(['search' => 'foo']);
$posts = Post::filter($filters);
return view('livewire.posts.index', ['posts' => $posts]);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.