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

ravipw1801's avatar

Laravel/SQL: How to fetch data from multiple table in a single query? that too using 'where'

Working on a search functionality on Laravel App(Blog/Posts). There are multiple types of posts (each having a separate table in the database) Like Business posts, Social Life posts etc..

Below is the search function on SearchController

class SearchController extends Controller
{
public function search(Request $request, $query = null)
{
    if($query == null)
        return redirect()->route('home');

    $search = Business::where([['title','like','%'.$query.'%'],['status','=',1]])
                        ->orWhere([['description','like','%'.$query.'%'],['status','=',1]])                         
                        ->paginate(10);

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

So basically my question is how to add Social Life Post's table and some other tables also?

0 likes
6 replies
Snapey's avatar

You will have to either join all tables before searching, or execute the same search for each and merge the results.

Unfortunately having seperate tables for each will cause you this type of extra redundant work

ravipw1801's avatar

Waiting for some replies/help? The issue has not yet been solved :(

varun-2301's avatar

is there any common column in all the posts table i.e in your case business post,social post etc?

ravipw1801's avatar

Yes, there is a common column "brand", each type of posts will have "brand" in the table.

Please or to participate in this conversation.