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.
This package is auto-updated.
Last update: 2025-08-21 22:36:41 UTC
README
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
- 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
Install the package via Composer:
composer require eaitfakir/eloquent-searchable
Usage
Adding Searchable Fields
- Include the
Searchable
trait in your model. - Define the
$searchable
property in your model to specify the fields you want to search.
Example:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Eaitfakir\EloquentSearchable\Traits\Searchable; class User extends Model { use Searchable; protected $searchable = ['name', 'email', 'address']; }
Performing a Search
You can now perform a search using the search
scope:
$users = User::search('John')->get();
This will search for the term 'John'
in the fields specified in the $searchable
property (name
, email
, and address
in this example).
You can use the searchExact
scope to search for an exact term:
$users = User::searchExact('John')->get();
This will search for the exact term 'John'
in the fields specified in the $searchable
property (name
, email
, and address
in this example).
You can use the searchByKeywords
scope to search for multiple keywords:
$users = User::searchByKeywords('John Doe')->get();
This will search for the keywords 'John'
and 'Doe'
in the fields specified in the $searchable
property (name
, email
, and address
in this example).
To perform a case-insensitive search, you can use the searchByKeywords
or search
scope with the $insensitive
parameter set to true
:
$users = User::searchByKeywords('John Doe', true)->get();
This will perform a case-insensitive search for the keywords 'John'
and 'Doe'
in the fields specified in the $searchable
property (name
, email
, and address
in this example).
To perform a search for a term in the relations provided, you can use the searchWithRelations
scope:
$users = User::searchWithRelations('John', ['posts' => ['title', 'content']])->get();
This will search for the term 'John'
in the user fields (name
, email
, and address
in this example) and search for the term 'John'
in the posts
relation (title
and content
in this example).
To perform a search for a term in the searchable fields using fuzzy matching, you can use the fuzzySearch
scope:
$users = User::fuzzySearch('Johb')->get();
This will perform a fuzzy search for the term 'Johb'
in the fields specified in the $searchable
property (name
, email
, and address
in this example).
To perform a search with weighted search on the specified fields, you can use the weightedSearch
scope:
$users = User::weightedSearch('John', ['name' => 2, 'email' => 1])->get();
This will perform a weighted search for the term 'John'
in the fields specified in the $searchable
property (name
with weight 2 and email
with weight 1 in this example).
Contributing
Contributions are welcome! To contribute:
- Fork this repository.
- Create a new branch:
git checkout -b feature/feature-name
. - Make your changes and commit:
git commit -m "Add new feature"
. - Push the changes:
git push origin feature/feature-name
. - Submit a pull request.
Please ensure all tests pass before submitting your PR.
License
This package is licensed under the MIT License. You’re free to use, modify, and distribute it as per the terms of the license.
Credits
Developed and maintained by EL MEHDI AIT FAKIR.