Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

Multiple Models Search: into One Collection with Pagination

February 17, 2023
5 min read

In this tutorial, we will create a simple search from three Models and will use Laravel Collections to combine them into one collection to show results in the front-end.

Also, we will make the table row background color to be based on the Model. And finally, we will add pagination to the Collection to show results with the pagination links.

In the end, we will have a similar result to this:

finished result


The structure of the database here is very simple. We have three models Post, Video, and Course, and each has a title column. Seeded data would look similar to this:

seeded data

Now for the controller where the search happens. In the search form, input has the name of query and because the form uses the GET method, after submitting the form we are getting to URL similar to /search?query=. In the controller, we can get the query parameter using Request. Using all of this, we can search this:

use Illuminate\Http\Request;
 
class SearchController extends Controller
{
public function __invoke(Request $request)
{
$posts = Post::where('title', 'like', '%' . $request->input('query') . '%')->get();
$courses = Course::where('title', 'like', '%' . $request->input('query') . '%')->get();
$videos = Video::where('title', 'like', '%' . $request->input('query') . '%')->get();
}
}

Now that we have all results...

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

AF
Attila Fulop ✓ Link copied!

This is good and works, but has one downside, that it loads all the data in the memory. It somewhat defeats the purpose of pagination, and has a performance penalty at larger datasets.

Any idea/tip about how to only load the records that are needed for the current page?

We're running a few ideas, but maybe you also have one - it worth a question :)

PK
Povilas Korop ✓ Link copied!

Yes, I agree that it loads all the data. Well, you can load not all the data, but only 10 records from each type, for example, where 10 is max records per page you would show anyway.

But generally, there's no universal good solution for this, at least I haven't experienced it.

SP
Sylvain P ✓ Link copied!

thanks a lot, have you a repo for this ?

AF
Attila Fulop ✓ Link copied!

This package does way more than just "zipping" multiple model types, but it does the job quite well: https://github.com/protonemedia/laravel-cross-eloquent-search

G
GaMini07 ✓ Link copied!

Hello. Can you tell me what color theme this is? I would like to use the same in my Phpstorm. Thanks.

PK
Povilas Korop ✓ Link copied!

These screenshots are not from PhpStorm, they are from Torchlight syntax highlighter.

In my PhpStorm, I use a theme called Material Darker.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.