Eloquent merge two queries

Published 4 months ago by cunlayogd

Hi all, how can I merge this two queries into a single query.

$jobLocations = DB::table('job_post_locations')
                     ->join('locations', 'job_post_locations.location_id', '=', 'locations.id')
                     ->select('locations.id','locations.name',DB::raw('count(*) as location_count'))
                     ->groupBy('location_id')
                     ->get();

        $jobIndustries = DB::table('job_post_industries')
                     ->join('industries', 'job_post_industries.industry_id', '=', 'industries.id')
                     ->select('industries.id','industries.name',DB::raw('count(*) as industry_count'))
                     ->groupBy('industry_id')
                     ->get();
mballaag

@cunlayogd First of all can you put your code in the ``` so everyone can actually read this: https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code

cunlayogd

@mballaag thank you, I have formatted the code

Resin

Use unions https://laravel.com/docs/5.6/queries#unions

$first = DB::table('users')
            ->whereNull('first_name');

$users = DB::table('users')
            ->whereNull('last_name')
            ->union($first)
            ->get();

Please sign in or create an account to participate in this conversation.