Union Query in Laravel 5

Sometimes you need to bind multiple queries then you can use union in laravel. If you use union then laravel query builder provide union method for mysql union. When you are doing big project or ERP level project then mostly you require using union for getting data from database with multiple tables.

Tips: The unionAll method is also available and has the same method signature as union.

$schools = DB::select("SELECT id, name as school_name, '' as
             group_name, '' as groupdate, address, expiry_date as
             schooldate,suspended,school_custome_id as groupschoolcutomid 
             FROM school where group_id = '' AND suspended = '0'
             UNION 
             SELECT group_id as id, '' as school_name, group_name, '' as address,
              '' as groupdate, expiry_date as schooldate,
              suspended,group_custome_id as groupschoolcutomid
               FROM groups where suspended = '0' ");  

Leave a Reply

Your email address will not be published. Required fields are marked *