youssefreda / searchable
Searchable trait for Laravel
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/youssefreda/searchable
Requires
- php: ^8.2
- laravel/framework: ^11.0|^12.0
This package is auto-updated.
Last update: 2025-11-04 07:17:23 UTC
README
A flexible and developer-friendly trait for Laravel Eloquent models that enables smart, customizable search functionality across model columns and relationships. Perfect for building dynamic filters and admin panels.
๐ฆ Installation
composer require youssefreda/searchable:@dev
Note: This version uses
@devstability flag โ make sure your project allows it incomposer.jsonor add:
๐ Features
- ๐ Search directly on model fields
- ๐ Supports searching across relationships (
belongsTo,hasMany, etc.) - ๐ข Smart numeric vs. string search
- ๐ Handles Arabic text search using
utf8mb4 - โ๏ธ Define custom labels, types, and visibility
- ๐ฅ Auto-generate dropdown options for searchable fields
๐ ๏ธ Usage
1. Use the Trait in Your Model
use Searchable\Traits\Searchable; class Apartment extends Model { use Searchable; }
2. Define $searchable Property
protected static $searchable = [ 'client_name' => 'Client Name', 'cost' => 'Cost', 'user' => [ 'label' => 'User', 'relation' => 'user', 'field' => 'full_name' ] ];
Field Configuration Options:
| Key | Type | Description |
|---|---|---|
label |
string | (Optional) Display label for UI dropdown |
relation |
string | (Optional) Relation name (belongsTo, etc.) |
field |
string | Column to search within the relation |
type |
string | 'number' to enforce numeric comparison |
๐ Search Examples
Global Search
Apartment::search('Ahmed')->get();
Column-Specific Search
Apartment::search('Ahmed', 'client_name')->get();
๐๏ธ Generating Dropdown Options
Use getSearchColumnOptions() to generate ['key' => 'label'] pairs:
$options = Apartment::getSearchColumnOptions();
Output:
[
'client_name' => 'Client Name',
'cost' => 'Cost',
'user' => 'User'
]
Blade Integration Example
@php $columns = \App\Models\Apartment::getSearchColumnOptions(); @endphp <select name="column" class="form-select"> <option value="all">{{ __('All Fields') }}</option> @foreach($columns as $value => $label) <option value="{{ $value }}" {{ request('column') === $value ? 'selected' : '' }}> {{ $label }} </option> @endforeach </select>
โ This snippet will allow the user to choose which column to search in. The list is auto-generated from the model's
$searchableproperty.
๐งช Example Model: Apartment
use App\Traits\Searchable; class Apartment extends Model { use Searchable; protected static $searchable = [ 'client_name' => 'Client Name', 'floor' => 'Floor', 'commission' => 'Commission', 'user' => [ 'label' => 'User', 'relation' => 'user', 'field' => 'full_name' ], 'project' => [ 'label' => 'Project', 'relation' => 'project', 'field' => 'name' ] ]; public function user() { return $this->belongsTo(User::class); } public function project() { return $this->belongsTo(Project::class); } }
โ๏ธ How It Works
| Type | Logic |
|---|---|
| String | LIKE %term% |
| Numeric | = + ABS(ROUND(...)) < 0.0001 for float-safe comparison |
| Relation | Uses orWhereHas on the defined relation.field |
| Arabic | Automatically converts field using utf8mb4 when needed |
๐ง Smart Numeric Detection
You can either:
- Explicitly define the type using
'type' => 'number' - Or let it auto-detect based on field name patterns (e.g.,
cost,amount,price, etc.)
๐ License
MIT ยฉ Youssef Reda
๐ค Contribute
Pull Requests and Issues are welcome!