Support the ongoing development of Laravel.io →
Database Eloquent Requests

I am trying to merge two queries

$sales = DB::table('sales')
    ->join('drugs', 'drugs.id', '=', 'sales.drug_id')
    ->select('sales.*','drugs.name', DB::raw('sum(sales.quantity_sold) as total_sales'))
    ->whereYear('date_sold','>','2014')
    ->whereYear('date_sold','<','2017')
     ->get()->groupBy('sales.drug_id');

$stocks = DB::table('sales')
     ->join('drugs', 'drugs.id', '=', 'sales.drug_id')
     ->select('sales.*','drugs.name', DB::raw ('AVG(sales.quantity_sold) as average_sales'))
     ->whereMonth('date_sold','=', Carbon::today()->month)
      ->get()->groupBy('sales.drug_id');

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

I get an error, Call to a member function groupBy() on a non-object. Any help?

Last updated 3 years ago.
0

try calling groupBy() before the get() instead of after

0

astroanu said:

try calling groupBy() before the get() instead of after

still doesn't work.. error Call to a member function merge() on a non-object

0

Sign in to participate in this thread!

PHPverse

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.