2016-12-02 1 views
4

한 브랜드와 한 시즌을 연결하는 패브릭 테이블이 있습니다. App\fabrics::with('brand', 'season')->get()을 실행하면 나에게 모든 직물과 브랜드 및 시즌 정보가 표시됩니다 (아래 참조).Laravel 5.3 상위 모델에서 테이블 조인에 Eloquent where 절

시즌 ID를 기반으로 원단을 얻고 싶습니다. 나는 시도 :

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

을하지만이 작동하지 않습니다.

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

관계를 통해 직물과 필터를 얻을 수 있습니까?

분명히 계절 모델 App :: season에서 직물을 얻을 수 있지만 부모 (직물) 모델에서 얻을 수 있는지 궁금합니다.

App\fabrics::with(['brand', 'season' => function($q) use($seasonId) { 
    $q->where('id', $seasonId); 
}])->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", 
     }, 
     }, 
    ], 
    } 

답변

3

사용 eager loads contraining에 대한 폐쇄 - ('브랜드', '계절')와 :: 앱 \ 직물에서

모든 정보 시즌 ID별로 패브릭을 필터링하려면 whereHas() :

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

+1 설명서 링크를 보려면 어떤 기능이 호출되었는지 잘 모릅니다. 그래서 도움이됩니다. with()에 여러 개의 클로저를 전달할 수 있습니까? 예를 들어 계절 ID로 필터링하고 브랜드 ID로 필터링 할 수 있습니까? – twigg

+1

물론 모든 관계에 대해 클로저를 사용할 수 있습니다. –

+0

훌륭합니다. $ seasonId 변수는 Eloquent에 의해 만들어졌습니다. – twigg