waxwink / search-repository
An abstraction repository class for laravel which handles the filtering and searching through a table
0.9.12
2019-12-02 17:22 UTC
Requires
- laravel/framework: >=5.7
This package is auto-updated.
Last update: 2025-05-05 02:16:08 UTC
README
Instruction
create a specific repository for your model (e.g User) and extend the SearchRepository. In your class you should define your filterable and searchable attributes and you are ready to go.
use Waxwink\SearchRepository\SearchRepository; class UserSearchRepository extends SearchRepository { protected $searchable_attributes = [ 'name', 'last_name', 'cellphone', ]; protected $filterable_attributes = [ 'birth_date', 'gender', 'name', 'last_name', 'cellphone', 'email', 'created_at' ]; public function __construct() { $this->query = App\User::query(); } }
After creating the search repository class it can be used in your controller as follows while using the SearchTrait
:
use Waxwink\SearchRepository\Concerns\SearchTrait; use Illuminate\Http\Request; class UserController { use SearchTrait; public function index(Request $request, UserSearchRepository $userSearchRepository) { $users = $this->filterAndSearch($request, $userSearchRepository)->paginate(); // ... } }
Request Body
The request body should contain the fields that are going to be filtered like the following:
{ "gender":["m"], "created_at":"2019-01-01", "birth_date":["1999-01-01", ">"], "name":["joe", "like"] }
Search Param
Search param should contain the searching key words like:
app/users?search=jack