简体   繁体   中英

Select Multiple table on Laravel

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 在此处输入图像描述

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在此处输入图像描述

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('........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM