A simple wildcard search on eloquent models.
zesty-bus/laravel-simple-search is a Laravel package for a simple wildcard search on eloquent models..
It currently has 2 GitHub stars and 105 downloads on Packagist (latest version 2.0.1).
Install it with composer require zesty-bus/laravel-simple-search.
Discover more Laravel packages by zesty-bus
or browse all Laravel packages to compare alternatives.
Last updated
A simple wildcard search on eloquent models.
Via Composer
composer require zesty-bus/laravel-simple-search
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);
}
}
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();
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. Please see the license file for more information.