mawuekom/laravel-searchable

Laravel package to make easy search on Eloquent model

v1.2.0 2022-04-21 14:31 UTC

This package is auto-updated.

Last update: 2024-04-21 18:54:36 UTC


README

This package is provided with a service provider and trait to make search on your eloquent model

Installation

You can install the package via composer:

composer require mawuekom/laravel-searchable

Usage

Laravel

Go to config/app.php, and add this in the providers key

'providers' => [
    ...
    Mawuekom\LaravelSearchable\LaravelSearchableServiceProvider::class,
    ...
]

Lumen

Go to bootstrap/app.php, and add this in the specified key

// Add provider 
$app->register(Mawuekom\LaravelSearchable\LaravelSearchableServiceProvider::class);

Your model which inherits an eloquent model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    ...

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'content'
    ];

     ...
}

Use it to make search

use App\Models\Post;

Post::whereLike(['title', 'content'], 'Post title');

You can also include Searchable Trait in your model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mawuekom\LaravelSearchable\Searchable;

class Post extends Model
{
    use Searchable;

    ...
}

Once done, you can make your reseach like this

use App\Models\Post;

Post::search(['title', 'content'], 'Post title') ->get();

Once done, enjoy it.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security and Report bug

If you discover any security related issues, please email seddorephraim7@gmail.com or contact me on Twitter @ephraimseddor instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.