Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ziben69's avatar

Eloquent query | Search in multiple tables

Hello guys, I have question about multiple search query

I have in my Controller function:

 public function search(Request $request)
    {
        $search = $request->get('search');
        $results = DB::table('pages')->where('description', 'like', '%'.$search.'%')->where('visible',1)->paginate(6);

        return view('pages.search', ['results' => $results]);
    }

but I would like to search too in table 'subpages'. Can someone help me?

0 likes
2 replies
fylzero's avatar

@ziben69 You just need to join another table and search on that.

This should be close to what you're looking for...

public function search(Request $request)
{
    $search = $request->search;
    $results = DB::table('pages')
        ->leftJoin('subpages', 'pages.subpage_id', '=', 'subpages.id')
        ->where('description', 'like', '%'.$search.'%')
        ->where('visible',1)
        ->paginate(6);

    return view('pages.search', ['results' => $results]);
}
23 likes
goldtaste's avatar

Think you are probably looking to do a union query. Not sure how you do that in eloquent. But should be easy enough to find out.

Please or to participate in this conversation.