Support the ongoing development of Laravel.io →
Database Eloquent

I am trying to combine results of two queries: For my first query, dd($sales)

    array:3 [▼
      0 => {#328 ▼
         +"name": "Isoniazid,300mg"
         +"mos": 8
         +"drug_id": 1
         +"total_sales": "60"
    
       }
      1 => {#329 ▼
        +"name": "Dapsone,100mgs"
        +"mos": 10
        +"drug_id": 2
        +"total_sales": "72"
   
      }
       2 => {#330 ▼
        +"name": "Nevirapine,200mgs"
        +"mos": 6
        +"drug_id": 3
        +"total_sales": "45"
 
       }
    ]

For my second query: dd($stocks);

    array:3 [▼
      0 => {#328 ▼
        +"name": "Isoniazid,300mg"
        +"mos": 8
        +"drug_id": 1
        +"average_stocks": "28"
    
      }
      1 => {#329 ▼
        +"name": "Dapsone,100mgs"
        +"mos": 10
        +"drug_id": 2
        +"average_stocks": "36"
   
      }
      2 => {#330 ▼
        +"name": "Nevirapine,200mgs"
        +"mos": 6
        +"drug_id": 3
        +"average_stocks": "15"
 
      }
    ]

I want the result to be

    array:3 [▼
      0 => {#328 ▼
        +"name": "Isoniazid,300mg"
        +"mos": 8
        +"drug_id": 1
        +"total_sales": "60"
        +"average_stocks": "28"
    
      }
      1 => {#329 ▼
        +"name": "Dapsone,100mgs"
        +"mos": 10
        +"drug_id": 2
        +"total_sales": "72"
        +"average_stocks": "36"
   
      }
      2 => {#330 ▼
        +"name": "Nevirapine,200mgs"
        +"mos": 6
        +"drug_id": 3
         +"total_sales": "45"
        +"average_stocks": "15"
 
      }
    ]
         $sales = DB::table('inventories')
              ->join('drugs', 'drugs.id', '=', 'inventories.drug_id')
                     ->select('drugs.name','drugs.mos', 'inventories.drug_id',DB::raw ('sum(inventories.quantity_sold) as total_sales'), 
                    ->whereYear('inventories.complete_sold','>','2014')
                    ->whereYear('inventories.complete_sold','<','2017')
                    ->whereMonth('inventories.complete_sold','=', Carbon::today()->month)
                     ->groupBy('inventories.drug_id')
                     ->get();

                 $stocks = DB::table('inventories')
                    ->join('drugs', 'drugs.id', '=', 'inventories.drug_id')
                    ->select('drugs.name', DB::raw ('AVG(inventories.quantity_sold) as average_sales'))
     
                     ->whereBetween('complete_sold', array('2016-11-01', '2016-12-31' ))
                    ->groupBy('inventories.drug_id')

                     ->get();
    $data=$sales->merge($stocks);

This however doesn't work, please help

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Gertie1 gertie1 Joined 28 Oct 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.