【问题标题】:Laravel 5.2 : How to get belongsTo through model?Laravel 5.2:如何通过模型获取 belongsTo?
【发布时间】:2017-03-19 15:53:19
【问题描述】:

我有带有car_idyearlicense_plate 字段的vehicles 模型。

我有 Car 模型和 car_class_idcar_brand_idname 字段。 车名包含类似(思域、帕杰罗等)

我有 Car_brandsname 字段。 (本田、丰田)

我有 Car_classesname 字段。 (SUV、MPV、卡车等)

vehicles 模型中的car_id 属于Car 模型。

car_class_id 属于car_classescar_brand_id 属于car_brands

如何获取具有 car_class 名称 MPV 的车辆列表?

我的关系是好的设计还是我只是将汽车品牌名称和汽车类别名称放在车辆表上?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    解决办法一定是这样的

    Vehicle::whereHas('car' , function($query) {
        $query->whereHas('car_classes' ,function($query2) {
            $query2->where('name', 'MPV');
        });
    })->get();
    

    【讨论】:

      【解决方案2】:

      你可以试试:

      Vehicle::whereHas('car' , function($q) {
          $q->whereHas('car_classes' ,fucntion($q) {
              $q->where('name', 'MPV');
          });
      })->get();
      

      【讨论】:

        猜你喜欢
        • 2017-06-07
        • 2019-02-15
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 1970-01-01
        • 2020-01-15
        • 2020-12-24
        • 2017-03-04
        相关资源
        最近更新 更多