PHPackages                             youssefreda/searchable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. youssefreda/searchable

ActiveLibrary[Search &amp; Filtering](/categories/search)

youssefreda/searchable
======================

Searchable trait for Laravel

01PHP

Since Jul 23Pushed 9mo agoCompare

[ Source](https://github.com/youssefreda4/searchable-package)[ Packagist](https://packagist.org/packages/youssefreda/searchable)[ RSS](/packages/youssefreda-searchable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

🔍 Laravel Searchable Trait
==========================

[](#-laravel-searchable-trait)

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
--------------

[](#-installation)

```
composer require youssefreda/searchable:@dev

```

> Note: This version uses `@dev` stability flag — make sure your project allows it in `composer.json` or add:

---

🚀 Features
----------

[](#-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
--------

[](#️-usage)

### 1. Use the Trait in Your Model

[](#1-use-the-trait-in-your-model)

```
use Searchable\Traits\Searchable;

class Apartment extends Model
{
    use Searchable;
}
```

---

### 2. Define `$searchable` Property

[](#2-define-searchable-property)

```
protected static $searchable = [
    'client_name' => 'Client Name',
    'cost' => 'Cost',
    'user' => [
        'label' => 'User',
        'relation' => 'user',
        'field' => 'full_name'
    ]
];
```

#### Field Configuration Options:

[](#field-configuration-options)

KeyTypeDescription`label`string(Optional) Display label for UI dropdown`relation`string(Optional) Relation name (`belongsTo`, etc.)`field`stringColumn to search within the relation`type`string`'number'` to enforce numeric comparison---

🔍 Search Examples
-----------------

[](#-search-examples)

### Global Search

[](#global-search)

```
Apartment::search('Ahmed')->get();
```

### Column-Specific Search

[](#column-specific-search)

```
Apartment::search('Ahmed', 'client_name')->get();
```

---

🎛️ Generating Dropdown Options
------------------------------

[](#️-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

[](#blade-integration-example)

```
@php
    $columns = \App\Models\Apartment::getSearchColumnOptions();
@endphp

    {{ __('All Fields') }}
    @foreach($columns as $value => $label)

            {{ $label }}

    @endforeach

```

> ✅ This snippet will allow the user to choose which column to search in. The list is auto-generated from the model's `$searchable` property.

---

🧪 Example Model: Apartment
--------------------------

[](#-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
---------------

[](#️-how-it-works)

TypeLogicString`LIKE %term%`Numeric`=` + `ABS(ROUND(...)) < 0.0001` for float-safe comparisonRelationUses `orWhereHas` on the defined `relation.field`ArabicAutomatically converts field using `utf8mb4` when needed---

🧠 Smart Numeric Detection
-------------------------

[](#-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
---------

[](#-license)

MIT © [Youssef Reda](https://github.com/youssefreda4)

---

🤝 Contribute
------------

[](#-contribute)

Pull Requests and Issues are welcome!

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance40

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/92baf755a4c32be46cb579cd1646f0e6ab8599218970418f5fbdf9e7f29da3bf?d=identicon)[youssefreda4](/maintainers/youssefreda4)

---

Top Contributors

[![youssefreda4](https://avatars.githubusercontent.com/u/154443732?v=4)](https://github.com/youssefreda4 "youssefreda4 (10 commits)")

### Embed Badge

![Health badge](/badges/youssefreda-searchable/health.svg)

```
[![Health](https://phpackages.com/badges/youssefreda-searchable/health.svg)](https://phpackages.com/packages/youssefreda-searchable)
```

###  Alternatives

[ruflin/elastica

Elasticsearch Client

2.3k50.4M202](/packages/ruflin-elastica)[opensearch-project/opensearch-php

PHP Client for OpenSearch

15024.3M64](/packages/opensearch-project-opensearch-php)[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[massive/search-bundle

Massive Search Bundle

721.4M13](/packages/massive-search-bundle)[shyim/opensearch-php-dsl

OpenSearch/Elasticsearch DSL library

175.9M9](/packages/shyim-opensearch-php-dsl)[outl1ne/nova-multiselect-filter

Multiselect filter for Laravel Nova.

45802.7k3](/packages/outl1ne-nova-multiselect-filter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
