Reputation: 335
I want to print out all the orders containing the products. I can not figure out how to build the sql query to return every order once. As you can see it returns the order with id 4 twice because it has 2 products in it.
Controller
$orders = DB::table('order')
->join('customer', 'order.customer_id', '=', 'customer.id')
->leftjoin('order_meal', 'order.id', '=', 'order_meal.order_id')
->select('order.*', 'customer.firstname', 'customer.lastname', 'customer.country', 'customer.city', 'customer.zipcode', 'customer.address', 'customer.phone', 'order_meal.id AS order_meal_id')
->where('order.restaurant_id', '=', $restaurantID)
->orderBy('order.created_at', 'ASC')
->get();
if ($orders === null) {
return redirect('/');
}
return view('/pages/orders', [
'pageConfigs' => $pageConfigs, 'orders' => $orders
]);
View: {{ $orders }}
The inserted data as JSON
{ "restaurant_id": 1, "customer_id": 1, "is_delivery": 1, "is_online_payment": 1, "meal": [ { "meal_id": 23, "quantity": 1, "extras": [ { "extra_id": 10 } ] }, { "meal_id": 24, "quantity": 1, "extras": [ { "extra_id": 13 } ] } ] }
Result [{"id":4,"customer_id":1,"restaurant_id":1,"is_delivery":1,"delivery_price":0,"coupon":null,"coupon_sale":"0","is_online_payment":1,"total_price":0,"is_paid":0,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05","firstname":"Dominik","lastname":"Balogh","country":"Magyarorsz\u00e1g","city":"Szentendre","zipcode":2000,"address":"Vxxxxxxxxxxxxxxxx 7.","phone":"06303900000","order_meal_id":80},
{"id":4,"customer_id":1,"restaurant_id":1,"is_delivery":1,"delivery_price":0,"coupon":null,"coupon_sale":"0","is_online_payment":1,"total_price":0,"is_paid":0,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05","firstname":"Dominik","lastname":"Balogh","country":"Magyarorsz\u00e1g","city":"Szentendre","zipcode":2000,"address":"Vxxxxxxxxxxxxxxxx 7.","phone":"06303900000","order_meal_id":81}]
The result shoul look like this:
[{"id":4,"customer_id":1,"restaurant_id":1,"is_delivery":1,"delivery_price":0,"coupon":null,"coupon_sale":"0","is_online_payment":1,"total_price":0,"is_paid":0,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05","order_meal":[{"id":80,"order_id":4,"meal_id":23,"quantity":1,"price":1850,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05"},{"id":81,"order_id":4,"meal_id":24,"quantity":1,"price":1890,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05"}]}]
Upvotes: 1
Views: 1012
Reputation: 335
I found a solution! You have to set the relations in the models!
order.php Model
class Order extends Model
{
public $table = "order";
protected $fillable = [
'customer_id',
'restaurant_id',
'is_delivery',
'delivery_price',
'coupon',
'coupon_sale',
'is_online_payment',
'total_price',
'is_paid'
];
public function ordermeal(){
return $this->hasMany('App\OrderMeal','order_id','id');
}
public function customer(){
return $this->hasOne('App\Customer','id','customer_id');
}
}
ordermeal.php Model
class OrderMeal extends Model
{
public $table = "order_meal";
protected $fillable = [
'order_id',
'meal_id',
'quantity',
'price'
];
public function order(){
return $this->belongsTo('App\Order','order_id','id');
}
public function ordermealextras(){
return $this->hasMany('App\OrderMealExtras','order_meal_id','id');
}
public function meal(){
return $this->hasOne('App\Meal','id','meal_id');
}
}
Controller
$orders = Order::where('restaurant_id', '=', $restaurantID)->orderBy('created_at', 'ASC')
->with('customer')
->with('ordermeal.meal')
->with('ordermeal.ordermealextras')->get();
return view('/pages/orders', [
'pageConfigs' => $pageConfigs, 'orders' => $orders
]);
Upvotes: 1