PHPackages                             eaitfakir/eloquent-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. [Database &amp; ORM](/categories/database)
4. /
5. eaitfakir/eloquent-searchable

ActiveLibrary[Database &amp; ORM](/categories/database)

eaitfakir/eloquent-searchable
=============================

Laravel Eloquent Searchable is a lightweight package that provides dynamic and flexible search functionality for your Eloquent models. Simply define searchable fields and perform quick searches across your data.

v1.0.0(1y ago)02MITPHP

Since Jan 4Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/eaitfakir/eloquent-searchable)[ Packagist](https://packagist.org/packages/eaitfakir/eloquent-searchable)[ RSS](/packages/eaitfakir-eloquent-searchable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

Laravel Eloquent Searchable
===========================

[](#laravel-eloquent-searchable)

**Laravel Eloquent Searchable** is a lightweight package that provides dynamic and flexible search functionality for your Eloquent models. Simply define searchable fields and perform quick searches across your data.

Features
--------

[](#features)

- **Dynamic Searchable Fields**: Define custom searchable fields in each model.
- **Seamless Integration**: Fully compatible with Laravel's query builder and Eloquent.
- **Customizable**: Easily extend or override default behavior for specific use cases.

---

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require eaitfakir/eloquent-searchable
```

---

Usage
-----

[](#usage)

### Important: Fields resolution and validation

[](#important-fields-resolution-and-validation)

All scopes that rely on fields will resolve the list of fields as follows:

- If you pass a non-empty `fields` array, it will be used.
- Otherwise, the model's `$searchable` property will be used.

If the resolved list is empty (no `fields` passed AND `$searchable` is not defined or empty), an `InvalidArgumentException` will be thrown with a helpful message:

> No searchable fields provided. Define a non-empty $searchable property on the model or pass a non-empty fields array to the scope.

This applies to: `search`, `exactMatch`, `keywordSearch`, `searchAcross` (for the base model fields), and `fuzzySearch`.

Additionally, `rankedSearch` validates that the `$weights` map is non-empty and will throw an `InvalidArgumentException` if it is empty.

### Adding Searchable Fields

[](#adding-searchable-fields)

1. Include the `Searchable` trait in your model.
2. Define the `$searchable` property in your model to specify the fields you want to search.

Example:

```
