Home > OS >  Select Multiple table on Laravel
Select Multiple table on Laravel

Time:07-13

I get the problem when I try to select table using DB on Laravel, in my controller nothing error, but when I try using dd(), the data still empty

SQL

SELECT monthname(od.created_at) AS bulan,  c.name AS name, round(SUM(od.price * od.qty * (100 - od.discount)/ 100),2) AS total
FROM order_details od, categories c, products p 
WHERE p.id = od.product_id 
AND p.category_id = c.id 
GROUP BY c.id, MONTH(od.created_at);

result on mysql enter image description here

Controller

$categories = DB::table(DB::raw('categories, order_details, products '))
                        ->select(DB::raw('monthname(order_details.created_at) AS bulan,  categories.name AS name, round(SUM(order_details.price * order_details.qty * (100 - order_details.discount)/ 100),2) AS total'))
                        ->fromRaw('categories, order_details, products ')
                        ->where('products.id', '=', 'order_details.product_id')
                        ->where('products.category_id', '=', 'categories.id')
                        ->groupByRaw('bulan, name')->get();

dd($categories); 

and this is the result enter image description here

CodePudding user response:

why do you wanna do like this ? we could use join.

DB::table('categories')->join('products','products.category_id','categories.id')
                 ->join('order_details','order_details.product_id','products.id)
                 ->select('........
  • Related