は私がカテゴリ、季節と映画と呼ばれる3つのテーブルを持っています。クエリ - laravel
カテゴリテーブル
+----+---------+------------+-------------+ | id | name | slug | created_at | +----+---------+------------+-------------+ | 1 | It | it-2017 | SOME TIME | | 2 | Another | another | SOME TIME | | 3 |SpiderMan| spider-man | SOME TIME | | 4 | Cars 3 | cars-3 | SOME TIME | +----+---------+------------+-------------+
季節のテーブル
+----+---------+------------+-------------+ | id | number |category_id | created_at | +----+---------+------------+-------------+ | 1 | 1 | 2 | SOME TIME | | 2 | 2 | 2 | SOME TIME | | 3 | 1 | 3 | SOME TIME | | 4 | 3 | 1 | SOME TIME | +----+---------+------------+-------------+
映画テーブル
+----+---------+------------+-------------+ | id | name | slug | season_id | +----+---------+------------+-------------+ | 1 | Pilot | pilot | 1 | | 2 | Second | second | 1 | | 3 | Third | third | 1 | | 4 | Fourth | fourth | 1 | +----+---------+------------+-------------+
そして私はそれを作っURLは、この
mywebpage.com/serie/{category_slug}/season-{season_number}
のようなものになるだろう。しかし、私はこのURLに行くところ、クエリはすべてのシーズンですべてのムービーを表示しようとしています。 現在のカテゴリのシーズンではありません。
マイコントローラー
public function getMovies($category_slug, $season_number)
{
$categories = Category::where('slug', $category_slug)->get();
$seasons = Season::with('movies')->where('number', $season_number)->get();
return view('routes.getMovies', compact('categories', 'seasons'));
}
そして、私のビューgetMovies.blade.php
@foreach($categories as $category)
@foreach ($seasons as $season)
@foreach ($season->movies as $movie)
Season - {{ $season->number }}
{{ $movie->name }}
{{ $movie->description }}
@endforeach
@endforeach
@endforeach
そして、私の出力は、すべてのカテゴリからシーズン1のようなものですが、私は現在の季節を示すことだけしたいですカテゴリ。
を - >( '数'、$のSEASON_NUMBERを) - >どこで( 'category_id'、$ categories-> id) - > get(); '!! – Maraboc
@Maraboc、それは私にエラーが発生します *このコレクションインスタンスにはIDがありません* – Ridiculous
この '$ category = Category :: where( 'slug'、$ category_slug) - > first() ; $ category_id、$ category-> id) - > get(); $ seasons =シーズン:: with( 'movies') - >ここで( 'number'、$ season_number) - > where( 'category_id'、$ category-> id) 戻るビュー( 'routes.getMovies'、compact( 'category'、 'seasons')); ' – Maraboc