2017-06-05 9 views
2

私はテーブルを持っていますStation & Equipment、それぞれのテーブルIDの他に追加フィールド "status"を持つ第3のピボットテーブルstation_equipmentステータスはアクティブでも非アクティブでもかまいません。しかし、私のクエリは両方の状態の機器を検索し続けます。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(); 
} 

Equimentモデル:

public function stations() 

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

これはあなたにStationワットを与える事前

答えて

1

で感謝i番目のID $id

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

これは、あなた$stationに関連付けられているすべてのアクティブなEquipmentsを与えるだろう。

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

どうすればステーションデータも取得できますか? –

+0

@RodrigoCabrera私は答えを編集しました。 –

関連する問題