Search through multiple Eloquent models with our latest Laravel package
There are many ways to add search functionality to your Laravel project. You can use the Query Builder, use the official Scout package to integrate with Algolia or use a third-party package like Spatie's laravel-searchable. Today we're introducing a new package to perform cross model, in-database searching. Here's a quick example how to perform a search query with our package:
$results = Search::add(Post::class, 'title') ->add(Video::class, 'title') ->get('howto');
We've made it really versitile and developer friendly. The example above is simple, but you can do really advanced searches as well:
$results = Search::add(Post::where('views', '>', 500), 'title', 'published_at') ->add(Video::with('tags')->published(), ['title', 'subtitle'], 'released_at') ->startWithWildcard() ->orderByDesc() ->paginate(25) ->get('howto');
We currently only support MySQL 5.7+, but we might add support for other drivers as well. So what can you expect from this package?
- Search through one or more Eloquent models.
- Support for cross-model pagination.
- Search through single or multiple columns.
- Use constraints and scoped queries.
- Eager load relationships for each model.
- In-database sorting of the combined result.
- Zero third-party dependencies.
- PHP 7.4 only, Laravel 6.0 and 7.0 supported, MySQL 5.7+ required.
The GitHub repository contains extensive documentation of all features. Follow me on Twitter for Laravel tips and package updates!