Searching Across Different Models in Laravel
Looking to search across multiple Eloquent models in Laravel, such as posts, videos, and courses? Laravel offers several ways to achieve this, be it through its in-built features or external packages.
Our Starting Point:
We've got three basic database tables:
Posts
Videos
Courses
Each of these tables corresponds to an Eloquent model. Our goal? To seamlessly search across these tables and pull combined results.
The Basics: Searching Across Three Models
Let's dive into the simplest method for searching across multiple models in Laravel. It might not be the fanciest, but it gets the job done.
Start with fetching the keyword from your request:
$keyword = request('keyword');
Next, run individual queries on each model to gather your results: