Laravel Helpers
View Package
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', 'roles.name' // 1.0.1 update now you can filter for relations name as well make sure that you have relation defined ]; // 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); });
v1.0.1 Update Example Usage
Category Model
<?php namespace App\Models; use Dipesh79\LaravelHelpers\Traits\Filterable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Category extends Model { use HasFactory, Filterable; protected $guarded = []; protected array $filterable = ['name','subCategories.name']; public function subCategories(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(SubCategory::class); } }
SubCategory Model
<?php namespace App\Models; use Dipesh79\LaravelHelpers\Traits\Filterable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class SubCategory extends Model { use HasFactory, Filterable; protected $guarded = []; protected array $filterable = ['name','category.name']; public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Category::class); } public function blogs(): HasMany { return $this->hasMany(Blog::class); } }
Blog Model
<?php namespace App\Models; use Dipesh79\LaravelHelpers\Traits\Filterable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Blog extends Model { use HasFactory, Filterable; protected $guarded = []; protected array $filterable = ['title', 'subCategory.name', 'subCategory.category.name']; public function subCategory(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(SubCategory::class); } }
Now you can query for subcategory field like name from category model.
<?php use Illuminate\Support\Facades\Route; Route::get('/', function () { $categories = \App\Models\Category::with(['subCategories']); $categories->customFilter('Custom'); $subCategories = \App\Models\SubCategory::with(['category']); $subCategories->customFilter('Custom'); $blogs = \App\Models\Blog::with(['subCategory.category']); $blogs->customFilter('Custom'); // this will filter upto categories column named 'name' as defined in Blog Model return view('welcome'); });
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.