PACKAGE IN DEVELOPMENT, DO NOT USE YET
This package lets you perform fast and local searches on your Eloquent Models. You can search foreign columns of related models too.
The approach is based on a gist by Jonathan Reinink (https://gist.github.com/reinink/28bd174087e929ffa1f150e3fe8ffbfa).
You can install the package via composer:
composer require ditscheri/eloquent-search
class Podcast extends Model
{
use \Ditscheri\EloquentSearch\Searchable;
/**
* The attributes that are searchable.
*
* @var string[]
*/
protected array $searchable = [
'title', // make sure to add proper indexes to each of these columns
'description',
'author.first_name',
'author.last_name',
'comments.message',
'series.title',
'series.tags.name',
];
public function author()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function series()
{
return $this->belongsTo(Series::class);
}
}
class PodcastController
{
public function index(Request $request)
{
return Podcast::search($request->input('q', null))->paginate();
}
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.