PHPackages                             theupriser/laravel-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. theupriser/laravel-searchable

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

theupriser/laravel-searchable
=============================

Laravel Searchable

4.1(1y ago)175MITPHPPHP ^8.0

Since Mar 17Pushed 1y agoCompare

[ Source](https://github.com/theupriser/laravel-searchable)[ Packagist](https://packagist.org/packages/theupriser/laravel-searchable)[ Docs](https://github.com/maize-tech/laravel-searchable)[ GitHub Sponsors](https://github.com/maize-tech)[ RSS](/packages/theupriser-laravel-searchable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

   ![Social Card of Laravel Searchable](/art/socialcard-light.png)

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

[](#laravel-searchable-)

[![Latest Version on Packagist](https://camo.githubusercontent.com/70d6847b20823aecf39e471e0ce1794bc26789a5b0ec6830fa986969bb67bdb8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61697a652d746563682f6c61726176656c2d73656172636861626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-searchable)[![GitHub Tests Action Status](https://camo.githubusercontent.com/432616fbe01cffef5c243eb6b78a8eb35bdfda6907af1d1479d5c7fa8e42064c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d73656172636861626c652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-searchable/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b6399f05267f9aff9965a2d3db3615f79d9eb609128871f21560f51e58c7e5f6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d73656172636861626c652f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-searchable/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/170e923bcd0469a31bda29e60152c621475d74c52b417f86b79c60302c462473/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61697a652d746563682f6c61726176656c2d73656172636861626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-searchable)

Easily add weighted searches through model attributes and relationships.

This package currently supports `MySQL` and `PostgreSQL`.

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

[](#installation)

You can install the package via composer:

```
composer require maize-tech/laravel-searchable
```

You can publish the config file with:

```
php artisan vendor:publish --provider="Maize\Searchable\SearchableServiceProvider" --tag="searchable-config"
```

This is the content of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Default match weight
    |--------------------------------------------------------------------------
    |
    | The weight of all searched words which match at least one of the
    | list of searchable attributes.
    | Defaults to 1.
    |
    */

    'default_match_weight' => 1,
];
```

Usage
-----

[](#usage)

To use the package, add the `Maize\Searchable\HasSearch` trait to each model you want to make searchable.

Once done, you can implement the `getSearchableAttributes` abstract method by returning the list of attributes (or relationships' attributes) you want to search for.

You can also define the weight of each searchable attribute. If no weight is specified then `default_match_weight` will be taken from `config/searchable.php`.

Here's an example model including the `HasSearch` trait:

```
