2016-09-24 30 views
2

我試圖從多個表中的數據低於我如何通過laravel多維數組遍歷5.2

enter image description here

內容給出了表的表結構是

enter image description here

在上述情況下我只是想獲取獨特movie_id爲city_id = 1的陣列及以下

我的代碼給出
$movies=General_cities::with('cinemahall.showtime')->where('city_id',$selectedcity->city_id)->get(); 

return $movies[0]['cinemahall'][1]['showtime'][0]['movie_id']; 

其中$ selectedcity-> city_id爲「1」,我正在從上面的代碼串的所有數據。我也能夠顯示單movie_id,但我想從所有多維數組遍歷並收集所有唯一movie_id。

好心幫我,因爲這似乎是相當困難的事情。在上述情況下 我應該得到的movie_id陣列[1,2]。

回答

0

嘗試在你的模型中使用此功能

public function unique_movie_id() 
{ 

return $this->join('movies_cinemahall', 'movies_cinemahall.city_id','=','general_cities.id')->join('movies_showtime', 'movies_showtime.cinema_id','=','movies_cinemahall.cinema_id')->join('movies','movies.movie_id','=','movies_showtime.movie_id')->distinct('movies.movie_id')->select('movies.id')->get(); 

}