youssefreda/searchable
Composer 安装命令:
composer require youssefreda/searchable
包简介
Searchable trait for Laravel
README 文档
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!
youssefreda/searchable 适用场景与选型建议
youssefreda/searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 youssefreda/searchable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 youssefreda/searchable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-23