2017-09-25 4 views
1

は私がカテゴリ季節映画と呼ばれる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のようなものですが、私は現在の季節を示すことだけしたいですカテゴリ

+0

を - >( '数'、$のSEASON_NUMBERを) - >どこで( 'category_id'、$ categories-> id) - > get(); '!! – Maraboc

+0

@Maraboc、それは私にエラーが発生します *このコレクションインスタンスにはIDがありません* – Ridiculous

+0

この '$ 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

答えて

1

まず、次に、以下は、特定の季節に、すべての映画を得ると確信して

作品モデル

public function season() 
{ 
    return $this->belongsTo(Season::class, 'season_id'); 
} 

シーズンモデル

public function category() 
{ 
    return $this->belongsTo(Category::class, 'category_id'); 
} 

あなたのモデルに権利関係を設定している作ります番号および特定のカテゴリスラッグ

$movies = Movie::whereHas('season', function ($season) use ($season_number) { 
    $season->where('number', $season_number); 
}) 
->whereHas('season.category', function ($category) use ($category_slug) { 
    $category->where('slug', $category_slug); 
}) 
->with('season') 
->get(); 

とビュー

@foreach ($movies as $movie) 
    Season - {{ $movie->season->number }} 
    {{ $movie->name }} 
    {{ $movie->description }} 
@endforeach 
0

でこのお試しください:この `$季節=シーズン::( '映画')で試してみてください

$categories = Category::where('slug', $category_slug)->first()->id; 

$seasons = Season::with('movies')->where('number', $season_number)->where('category_id', $categories)->get();