Search Across Multiple Eloquent Models With Cross-Eloquent Search

Packages

September 23rd, 2021

Search Across Multiple Eloquent Models With Cross-Eloquent Search

Laravel Cross-Eloquent Search is a package to search through multiple Eloquent models. This package supports sorting, pagination, scoped queries, eager load relationships, and searching through single or multiple columns:

// Returns \Illuminate\Database\Eloquent\Collection
Search::new()
->add(Post::class, 'title')
->add(Video::class, 'title')
->get('howto');
 
// Add multiple models at once
Search::addMany([
[Post::class, 'title'],
[Video::class, 'title'],
])->get('howto');

Often, you'll want to paginate results, and this package makes doing so a cinch:

Search::add(Post::class, 'title')
->add(Video::class, 'title')
->paginate()
// or
->paginate(perPage: 15, pageName: 'page', page: = 1)
->get('build');

The readme has tons of information about using this package that I'd recommend checking out:

  • Search through one or more Eloquent models
  • Support for cross-model pagination
  • Search through single or multiple columns
  • Order by (cross-model) columns or by relevance
  • Use constraints and scoped queries
  • Eager load relationships for each model
  • In-database sorting of the combined result
  • Zero third-party dependencies

You can learn more about this package, get full installation instructions, and view the source code on GitHub. The author of this package also wrote a blog post that goes into more detail about using this package.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.