【问题标题】:hasManyThrough relations ships with join?hasManyThrough 关系与 join?
【发布时间】:2021-06-09 22:57:31
【问题描述】:

我有 4 张桌子(CarsVignettesInspectionsInsurances)。小插图、检查和保险是“属于”汽车的。

我需要将这三个表的结果合并到一个查询中,并按“created_at”排序并取前 3-4 个。 我怎样才能做到这一点?

所有三个表都有相同的列名和类型

【问题讨论】:

    标签: laravel join eloquent relationship has-and-belongs-to-many


    【解决方案1】:

    您应该能够将事物链接在一起,例如

    $car->with('Vignettes')
     ->orderBy('created_at', 'desc)
     ->take(4)
     ->get();
    

    和 laravel 一样,the docs 是你的朋友。

    【讨论】:

    • 我试过这个: Car::find(1)->with('vignettes', 'insurances', 'inspections') ->orderBy('expire_on', 'desc') ->take (4) ->get();但不能正常工作。
    • 也许我需要一个数据透视表,或者只是将所有三个表添加到一个字段为“tax_type”的表中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2018-10-18
    • 2023-03-09
    • 2020-05-09
    • 2017-11-12
    • 2020-10-14
    相关资源
    最近更新 更多