0

我有你的桌子:Orders,,,ProductsProducts_images

我需要得到所有订单,为此我这样做:

$orders = Order::with("products")->where("user_id", Auth::guard('api')->user()->id)->orderBy('id')->get();

with("products")模型中的函数在哪里Order

public function products()
    {

        return $this->hasOne("App\Product", "id", "product_id");
    }

所以,我已经连接了两个表。我还需要在这个查询Products中用表连接表。Products_images

我怎样才能做到这一点?

4

1 回答 1

1

将 product_images 函数添加到 Products 模型

public function product_images() { return $this->hasMany("App\ProductImage");}

修改上述行的 App\ProductImage 以反映您的表的模型。然后,您可以通过执行以下操作访问属于您的产品的所有产品图像记录:

$orders = Order::with("products.product_images")->where("user_id", Auth::guard('api')->user()->id)->orderBy('id')->get();

查看此链接上的嵌套预加载:https ://laravel.com/docs/5.3/eloquent-relationships#eager-loading

于 2016-09-04T15:03:55.780 回答