2017-06-05 101 views
2

我有表,站&設備,具有附加字段第三透視表station_equipment「狀態」,除了各表的ID,狀態可爲有效或無效,我只是想獲得有源元件,但我的查詢繼續檢索具有兩種狀態的設備。Laravel從數據透視表檢索狀態

查詢:

$eqestacion = Station::where('station.id', $id) 
       ->whereHas('equipments', function($query) { 

        $query->where('equipment_station.status','=','active'); 
        })->with('equipments') 
         ->get(); 

站型號:

public function equipments(){ 

    return $this->belongsToMany('App\Equipment')->withPivot('status')->withTimestamps(); 
} 

廠房設備型號:

public function stations() 

    return $this->belongsToMany('App\Station') 
    ->orderBy('pivot_created_at','desc')->withPivot('status')->withTimestamps(); 
} 

在此先感謝

回答

1

這會給你一個Station瓦特第i個ID $id

$station = Station::findOrFail($id); 

這會給你相關的$station所有活動Equipments

$station->equipments()->wherePivot('status', 'active')->get(); 
+0

我怎麼能得到車站數據呢? –

+0

@RodrigoCabrera我編輯了答案。 –