【问题标题】:How get needed rows from 3 table in Laravel 5.5如何从 Laravel 5.5 中的 3 个表中获取所需的行
【发布时间】:2017-10-24 00:41:59
【问题描述】:

我有 3 个表,Order、Products、Order_Products。我需要从订单和产品中获取所有字段,可以使用 hasManyThrough()。但我还需要 Order_products 的 1 个字段。我怎样才能得到这个字段?

public function products()
{
   //in order model
 return $this->hasManyThrough('App\Models\Product','App\Models\OrderProduct','order_id','id','id','product_id');
}

使用 sql 我需要像这样的查询

选择 products.*, order_products.order_id, order_products.count as order_countproducts 内连接order_products ON order_products.product_id = products.id 在哪里 order_products.order_id = 2

【问题讨论】:

标签: php laravel


【解决方案1】:

您可以使用模型上的数据透视属性访问中间表字段。

假设你有产品,那么你可以访问 orders_products 的计数字段

$product->pivot->count;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2021-01-07
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2013-12-26
    相关资源
    最近更新 更多