I'm trying to convert a sql query to a Laravel Query builder, but i find hard understand which is the best way for FORM SUBQUERY in Laravel, because if i'm not wrong, is not something that you can normally do whit query builder, but you should implement in raw.
is this right?
and how should translate this?
SELECT t1.* FROM (//with out sub query it would be just products AS t1
SELECT SUM(amount)AS amount, products.*
FROM products ,orders
WHERE orders.product_id = products.product_id
GROUP BY orders.product_id )AS t1
LEFT OUTER JOIN products as t2
ON(t1.name = t2.name AND t1.year_production< t2.year_production)
WHERE t2.name is null
I could translate just with out adding the sub query
$poductlist =Product::from('products AS t1')->select('t1.*')
->leftjoin('products AS t2', function($join){
$join->on('t1.name','=', 't2.name')
->on('t1.year_production','<','t2.year_production')
->whereNull('t2.name')
->orderBy('t2.name','desc');})
->get();