简体   繁体   English

Laravel 5.3 Eloquent where子句从父模型加入表

[英]Laravel 5.3 Eloquent where clause on joining table from parent model

I have a fabrics table which links to one brand and one season. 我有一个织物桌,链接到一个品牌和一个季节。 Running App\\fabrics::with('brand', 'season')->get() gets me all the fabrics and their brand and season information which is fine (see below). 运行App\\fabrics::with('brand', 'season')->get()获取所有面料及其品牌和季节信息,这很好(见下文)。

I want to get fabrics based on the season id. 我想根据季节ID获得面料。 I tried: 我试过了:

App\\fabrics::with('brand', 'season')->where('season.id', '1')->get()

but this doesn't work. 但这不起作用。

Column not found: 1054 Unknown column 'season.id' in 'where clause' (SQL: select * from `fabrics` where `season`.`id` = 1)'

Is it possible to get fabrics and filter by one of its relationships? 是否可以通过其中一种关系获得面料和过滤器?

Obviously I can get the fabrics from the season model App::season but I was wondering if it can be achieved from its parent (fabrics) model. 显然我可以从季节模型App :: season获得面料,但我想知道它是否可以通过其母(面料)模型实现。

All the information from App\\fabrics::with('brand', 'season')->get() 来自App \\ fabrics :: with('brand','season') - > get()的所有信息

>>> App\fabrics::with('brand', 'season')->get()
=> Illuminate\Database\Eloquent\Collection {#794
     all: [
       App\Fabrics {#786
         id: 1,
         fabric_code: "GMAN01",
         fabric_design: "ANODA",
         fabric_price_group: "I",
         fabric_retail_price: "27.00",
         fabric_trade_price: null,
         fabric_width: 141,
         fabric_pattern_repeat: 1,
         fabric_fiber: "48% POLYESTER 44% COTTON 8% VISCOSE",
         fabric_online: 1,
         fabric_available: 1,
         fabric_meters_remaining: 256.78,
         fabric_virutal_meters_remaining: 216.28,
         created_at: "2016-12-02 10:52:09",
         updated_at: "2016-12-02 13:08:07",
         brand_id: 1,
         season_id: 1,
         brand: App\Brands {#742
           id: 1,
           brand_name: "*****",
           created_at: "2016-12-02 11:36:10",
           updated_at: "2016-12-02 11:36:10",
         },
         season: App\Seasons {#795
           id: 1,
           season_name: "Autumn/Winter 2016",
           season_code: "AW16",
           created_at: "2016-12-02 13:07:50",
           updated_at: "2016-12-02 13:07:50",
         },
       },
       App\Fabrics {#765
         id: 2,
         fabric_code: "GMAN02",
         fabric_design: "ANODA",
         fabric_price_group: "I",
         fabric_retail_price: "27.00",
         fabric_trade_price: null,
         fabric_width: 141,
         fabric_pattern_repeat: 1,
         fabric_fiber: "48% POLYESTER 44% COTTON 8% VISCOSE",
         fabric_online: 1,
         fabric_available: 0,
         fabric_meters_remaining: 20.0,
         fabric_virutal_meters_remaining: 17.0,
         created_at: "2016-12-02 10:52:09",
         updated_at: "2016-12-02 13:14:56",
         brand_id: 2,
         season_id: 1,
         brand: App\Brands {#770
           id: 2,
           brand_name: "*****",
           created_at: "2016-12-02 11:36:10",
           updated_at: "2016-12-02 11:36:10",
         },
         season: App\Seasons {#795},
       },
       App\Fabrics {#791
         id: 3,
         fabric_code: "JBBU01",
         fabric_design: "BURDOCK",
         fabric_price_group: "D",
         fabric_retail_price: "16.00",
         fabric_trade_price: null,
         fabric_width: 140,
         fabric_pattern_repeat: 64,
         fabric_fiber: "100% COTTON",
         fabric_online: 0,
         fabric_available: 1,
         fabric_meters_remaining: 856.1,
         fabric_virutal_meters_remaining: 856.1,
         created_at: "2016-12-02 10:52:09",
         updated_at: "2016-12-02 13:08:13",
         brand_id: 3,
         season_id: 4,
         brand: App\Brands {#787
           id: 3,
           brand_name: "*****",
           created_at: "2016-12-02 11:36:10",
           updated_at: "2016-12-02 11:36:10",
         },
         season: App\Seasons {#775
           id: 4,
           season_name: "Spring/Summer 2015",
           season_code: "SS15",
           created_at: "2016-12-02 13:07:50",
           updated_at: "2016-12-02 13:07:50",
         },
       },
     ],
   }

Use the closure for eager loads contraining : 使用封闭装置来控制急切负荷

App\fabrics::with(['brand', 'season' => function($q) use($seasonId) {
    $q->where('id', $seasonId);
}])->get();

If you want to filter fabrics by season ID, use whereHas() : 如果whereHas()季节ID过滤结构,请使用whereHas()

App\fabrics::with('brand', 'season')
    ->whereHas('season', function($q) use($seasonId) {
        $q->where('id', $seasonId);
    })->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM