tlodge
7 months ago

Displaying multiple laravel queries in one foreach loop

Posted 7 months ago by tlodge

I'm trying to query a database multiple times to then display the results in table. I've tried to merge the results but this doesn't seem to work in the for each loop.

What I'm trying to do is combine these in to a table where there is "user name" "weekly figures" "annual Figures" My two queries are:

    $annualfigures = DB::table("completedsales")
                        ->leftJoin('users', 'users.id', '=', 'completedsales.users_id')
                        
                        ->select('completedsales.users_id as users_id', 'users.name', DB::raw('SUM(completedsales.commission + completedsales.partnercommission + completedsales.mortcommission + completedsales.partnermortcommission) as takenpremium'))
                       
                        ->groupBy('users.name')
                        ->get();

$weeklyfigures = DB::table('completedsales')
                            ->leftJoin('users', 'users.id', '=', 'completedsales.users_id')
                            ->select('completedsales.users_id as users_id', 'users.name', 'completedsales.created_at', DB::raw('SUM(completedsales.commission + completedsales.partnercommission + completedsales.mortcommission + completedsales.partnermortcommission) as weeklypremium'))
                            ->whereBetween('completedsales.created_at', [$start_week, $end_week])
                            // ->groupBy('users_id')
                            // ->merge($users)
                            ->get();

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