The crossJoinSub method of the query constructor

Using the CROSS JOIN subquery

1use Illuminate\Support\Facades\DB;
2 
3$totalQuery = DB::table('orders')->selectRaw('SUM(price) as total');
4 
5DB::table('orders')
6 ->select('*')
7 ->crossJoinSub($totalQuery, 'overall')
8 ->selectRaw('(price / overall.total) * 100 AS percent_of_total')
9 ->get();

Tip given by @PascalBaljet

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord