zesty-bus/laravel-simple-search

A simple wildcard search on eloquent models.

2.0.1 2022-05-16 09:29 UTC

This package is auto-updated.

Last update: 2024-05-16 14:04:44 UTC


README

A simple wildcard search on eloquent models.

Installation

Via Composer

composer require zesty-bus/laravel-simple-search

Usage

Preparing your models

Add a searchable property. These are the table columns you'll be searching on. If the model has a relationship, you can use dot notation to determine the path.

namespace App\Models;

class Post extends Model
{
    protected $searchable = [
        'name', 'body', 'category.name'
    ];
    
    public function category()
    {
        return $this->hasOne(Category::class);
    }
}

Searching

To run a search use the search() method on your queries.

$posts = Post::search($request->query)->get();

You can override the default search columns by passing an array.

$posts = Post::search($request->query, ['name'])->get();

Config

You may want to change the search method name or the search property name on your models. First, publish the config.

php artisan vendor:publish --provider="ZestyBus\LaravelSimpleSearch\LaravelSimpleSearchServiceProvider" --tag="config"

Simply change the method name or property name to something that suits your requirements.

return [

    /**
     *  Name of the eloquent builder method.
     */
    'method' => 'filter',

    /**
     *  Name of the models public searchable property.
     */
    'property' => 'filterable'
];
$posts = Post::filter($request->query, ['name'])->get();
namespace App\Models;

class Post extends Model
{
    protected $filterable = [
        'name', 'body', 'category.name'
    ];
    
    public function category()
    {
        return $this->hasOne(Category::class);
    }
}

License

license. Please see the license file for more information.