PHP code example of dinhquochan / laravel-query-filters
1. Go to this page and download the library: Download dinhquochan/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/ */
dinhquochan / laravel-query-filters example snippets
namespace App\Http\Filters;
use DinhQuocHan\QueryFilters\QueryFilter;
class PostFilter extends QueryFilter
{
/**
* Filter by user id.
*
* @param int $id
* @return void
*/
public function userId($id)
{
$this->getQuery()->where('user_id', $id);
}
}
namespace App\Http\Controllers;
use App\Post;
use App\Http\Filters\PostFilter;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @param \App\Http\Filters\PostFilter $filter
* @return \Illuminate\Http\Response
*/
public function index(PostFilter $filter)
{
$posts = $filter->of(Post::class)->get();
// or $filter->of(Post::query())->get();
// or $filter->of(new Post())->get();
// Send it to view.
return view('posts.index', compact('posts'));
}
}
namespace App\Http\Filters;
use DinhQuocHan\QueryFilters\SearchableQueryFilter;
use DinhQuocHan\QueryFilters\QueryFilter;
class PostFilter extends QueryFilter
{
use SearchableQueryFilter;
/**
* Searchable columns.
*
* @var array
*/
protected $searchable = [
'id', 'title',
];
}
bash
php artisan make:filter "Blog/PostFilter"
> your-url?search=foo or your-url?q=foo
> SELECT * FROM `posts` WHERE (`id` LIKE '%foo%' OR `title` LIKE '%foo%')
> your-url?search=foo*
> SELECT * FROM `posts` WHERE (`id` LIKE 'foo%' OR `title` LIKE 'foo%')
> your-url?search=*foo
> SELECT * FROM `posts` WHERE (`id` LIKE '%foo' OR `title` LIKE '%foo')
// your-url?search=foo&search_by=title
// SELECT * FROM `posts` WHERE `title` LIKE '%foo%'
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.