如何使这个等式在 MYSQL 或 Laravel 中有效

Howto make this relation work in MYSQL or Laravel

我对 MYSQL 一点也不惊讶,但是 laravel 框架让我很容易得到我需要的东西,只要我了解基础知识。

所以这个问题将首先针对 MYSQL 人,因为我宁愿弄清楚如何在普通 MYSQL 中实现这一点,然后将其转换为 laravel 查询.

我有4个表需要用在这个查询中,如下所示:

1. products

    id | name | description | price

2. customers

    id | fname | lname | email | telephone

3. orders

    id | customer_id | total_price | status

4. order_items

    id | order_id | product_id | quantity | price

所以我正在我的网络应用程序上创建一个订单页面。在此页面上,它将显示所有订单的列表,包括该特定订单的 order_items。它还将包括该订单属于哪个客户。

我已经使用 laravel 实现了上述目标,我得到了如下所示的数组:

Array
(
[0] => Array
    (
        [id] => 9
        [customer_id] => 16
        [total_price] => 103.96
        [status] => new
        [created_at] => 2016-02-24 03:06:41
        [customer] => Array
            (
                [id] => 16
                [fname] => firstname
                [lname] => lastname
                [email] => firstname.lastname@gmail.com
                [telephone] => 07707707707
                [address_line_1] => Warstone Rd, walsall
                [address_line_2] => Wolverhampton 
                [postcode] => WV10 7LX
            )

        [order_item] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [order_id] => 9
                        [product_id] => 44
                        [quantity] => 1
                        [price] => 50.00
                    )

                [1] => Array
                    (
                        [id] => 2
                        [order_id] => 9
                        [product_id] => 46
                        [quantity] => 2
                        [price] => 31.98
                    )

                [2] => Array
                    (
                        [id] => 3
                        [order_id] => 9
                        [product_id] => 48
                        [quantity] => 1
                        [price] => 7.99
                    )

                [3] => Array
                    (
                        [id] => 4
                        [order_id] => 9
                        [product_id] => 51
                        [quantity] => 1
                        [price] => 13.99
                    )

            )

    )

)

现在我遇到的问题是获取与 order_items 相关的产品。

到目前为止它对我有用,因为我一直在这样思考

$order = Order::find($id)->with('customer','orderItem')->get()->toArray();

这很容易,因为一个订单有一个 customer_id 字段,一个 order_items 有一个 order_id。但是为了获得产品,我需要将产品加入 order_items。

如果有哪位擅长MYSQL,可以提供一个query给我研究一下,然后转换成laravel就好了。

如果有人知道 laravel 5,下面就是所有 laravel 内容:

Order.php


public function customer(){
    return $this->belongsTo('App\Customer');
}

public function orderItem(){
    return $this->hasMany('App\OrderItem');
}


OrderItem.php

public function order(){
    $this->belongsTo('App\Order');
}

public function product(){
    $this->hasOne('App\Product');
}


Product.php

public function orderitem(){
    $this->hasOne('App\OrderItem');
}


Customer.php

public function orders(){
    $this->belongsTo('App\Order');
}

如你所见,上面是我设置的模态关系。

这是我的控制器,我试图在其中获取完整阵列。

public function show($id)
{
    $order = Order::find($id)->with('customer','orderItem','product')->get()->toArray();

    echo "<pre>";
    print_r($order);
    echo "</pre>";
}

我收到的错误是:

 call to undefined method Illuminate\Database\Query\Builder::product()

如果我删除 ->with('customer','orderItem','product') 并将其更改为 ->with('customer','orderItem' ) 我得到上面发布的数组。

请问有人知道我该怎么做吗?

你是对的,你犯的唯一错误是你在订单模型上调用产品,与它没有直接关系。你必须像这样通过 orderitem 模型调用产品模型

   Order::find($id)->with('customer','orderItem.product')->get()->toArray()