Level 1
It looks like you want to do first a SUM(...) query to get the sum of 'qty' in the stocks table, something like SUM(qty) GROUP BY pid, then join this result to the products table, I'd say it would be something like
Product::leftJoin(
DB::raw('(SELECT SUM(qty) FROM stocks GROUP BY pid) as stock_sum'),
'stock_sum.pid', '=', 'products.id'
)
I'm not sure this would work, but the idea is you first sum the stocks table and group by ID, then join using this ID and the ID in the stocks table.