mercredi 12 décembre 2018

Laravel - filter join and groupby query by date

I spent a whole day attempting to do this in the wrong way. Now I manged to figure out how to utilize join tables but am having an issue with attempting to filter out certain rows.

Order and Product have many-to-many relationship ->withPivot(qty)

This code is currently working as expected. It collects, groups and sums the Product qtys:

$meals = DB::table('order_product')
   ->join('products', 'products.id', '=', 'order_product.product_id')
   #->join('orders', 'orders.id', '=', 'order_product.order_id')
   #->where('orders.updated_at', '>=', Carbon::now()->startOfWeek())
   ->groupBy('products.name', 'order_product.qty')
   ->select('products.name', DB::raw("SUM(qty) as total_qty"))
   ->get();

However, the part I commented out is meant to filter out any Order not created this week, but it does not. I do not even receive an error.

That exact code snippet (in a different context) does work in another part of my application though.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire