如何在laravel中打印多个产品的所有订单?

ygya80vv  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(268)

我想把所有的订单都打印出来。我不知道如何构建sql查询来返回每个订单一次。正如您所看到的,它返回id为4的订单两次,因为其中有2个产品。
控制器

$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
        ]);

查看:{{$orders}}
作为json插入的数据
{“餐厅id”:1,“顾客id”:1,“是送货”:1,“是在线付款”:1,“用餐”:[{“用餐id”:23,“数量”:1,“额外服务”:[{“额外服务id”:10}]},{“用餐id”:24,“数量”:1,“额外服务”:[{“额外服务id”:13}]}
结果[{“id”:4,“customer\u id”:1,“restaurant\u id”:1,“is\u delivery”:1,“delivery\u price”:0,“coupon”:null,“coupon\u sale”:“0”,“is\u online\u payment”:1,“total\u price”:0,“is\u payment”:0,“created\u at”:“2020-06-11 22:15:05”,“updated\u at”:“2020-06-11 22:15:05”,“firstname”:“dominik”,“lastname”:“balogh”,“country”:“magyarorsz\u00e1g”,“city”:“szentendre”,“zipcode”:2000,“address”:“vxx7.”,“phone”:“06303900000”,“order\u mean\u id”:80},
{“id”:4,“customer\u id”:1,“restaurant\u id”:1,“is\u delivery”:1,“delivery\u price”:0,“coupon”:null,“coupon\u sale”:“0”,“is\u online\u payment”:1,“total\u price”:0,“is\u payment”:0,“created\u at”:“2020-06-11 22:15:05”,“updated\u at”:“2020-06-11 22:15:05”,“firstname”:“dominik”,“lastname”:“balogh”,“country”:“magyarorsz\u00e1g”,“city”:“szendre”,“zipcode”:2000,“address”:“vxx7.”,“phone”:“06303900000”,“order\u mean\u id”:81}]
数据库客户表:

订单表:

订购餐台(产品)

结果应该是这样的:

[{"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"}]}]
tsm1rwdh

tsm1rwdh1#

我找到了解决办法!你必须在模型中设置关系!
order.php模型

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');
    }
}

ordermein.php模型

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');
    }
}

控制器

$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
        ]);

相关问题