• Reading time ~ 1 min
  • 10.09.2022

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.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297