Laravel Helpers
View Package Overview: Laravel Helpers provides a collection of utility functions to simplify and enhance your Laravel development.
Installation: Install the package via Composer:
composer require dipesh79/laravel-helpers
Filterable Trait
While querying the database, you might want to search within specific columns. You can use this trait for that purpose.
Use the Filterable Trait in your respective model and add the filterable property to the model as well.
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Dipesh79\LaravelHelpers\Traits\Filterable; class User extends Authenticatable { use Filterable; protected array $filterable = [ 'name', 'email', ]; // this is optional. Later you can pass on scope as well but if you have fixed columns to search then you can use this. }
Now, query the models:
<?php use Illuminate\Support\Facades\Route; Route::get('/', function () { $users = \App\Models\User::query(); $users = $users->customFilter('Stehr'); // this will search for name and email with this query. // Optionally you can also pass the columns which you want to filter on. return view('welcome',$users); }); // OR Route::get('/', function () { $users = \App\Models\User::query(); $users = $users->customFilter('Stehr',['name','email']); return view('welcome',$users); });
License: MIT
Author: @Dipesh79
Support: For support, email dipeshkhanal79[at]gmail[dot]com.
This post is licensed under CC BY
4.0 by the
author.