Created
April 3, 2019 22:44
-
-
Save DenoGeek/7d56fa41004eef60d3e27f143c6f81b4 to your computer and use it in GitHub Desktop.
crazy query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$orders = Order::join('assignments', 'orders.active_assignment', '=', 'assignments.id'); | |
$orders->join('users', 'assignments.user_id', '=', 'users.id'); | |
$orders->leftJoin('bargains', 'bargains.order_id', '=', 'orders.id'); | |
$orders->doesntHave('payment'); | |
$orders->where('orders.status',4); | |
$orders->select(['orders.id','bargains.order_id','users.id as user_id','orders.order_no','orders.salary','users.name',DB::raw('SUM(bargains.amount) As bargains_sum, (SUM(bargains.amount)+orders.salary) as total')]); | |
$orders->groupBy(['orders.id']); | |
$result = $orders->get(); | |
/* | |
* Group the accounts after lessening the DBMS work now this is after the query has executed | |
*/ | |
$grouped = $result->groupBy('user_id')->map(function ($row) { | |
return ['username'=>$row->first()->name,'bargains'=>$row->sum('bargains_sum'),'salary'=>$row->sum('salary'),'amount'=>$row->sum('total')]; | |
});; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment