PHPnews.io

Search through multiple Eloquent models with our latest Laravel package

Written by Pascal Baljet Media - Blog / Original link on Jul. 14, 2020

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?

The GitHub repository contains extensive documentation of all features. Follow me on Twitter for Laravel tips and package updates!

pascalbaljetmedia

« Weekly Update 197 - PHP Internals News: Episode 62: Saner Numeric Strings »