Search Across Multiple Eloquent Models With Cross-Eloquent Search


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
->add(Post::class, 'title')
->add(Video::class, 'title')
// Add multiple models at once
[Post::class, 'title'],
[Video::class, 'title'],

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

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

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.