繁体   English   中英

如何在 Laravel 中打印出包含多个产品的所有订单?

[英]How to print out all orders with multiple products in Laravel?

我想打印出包含产品的所有订单。 我不知道如何构建 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

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

结果[{"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\ág","city":"Szentendre","zipcode":2000,"address":"Vxxxxxxxxxxxxxxxx 7.","电话":"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\ág","city":"Szentendre","zipcode":2000,"address":"Vxxxxxxxxxxxxxxxx 7.","phone" :“06303900000”,“order_meal_id”: 81 }]

数据库客户表: 在此处输入图片说明

订单表: 在此处输入图片说明

order_meal 表(产品) 在此处输入图片说明

结果应该是这样的:

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

我找到了解决办法! 您必须在模型中设置关系!

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

ordermeal.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
        ]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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