【发布时间】:2017-09-25 09:24:00
【问题描述】:
我有 3 个表格,分别称为 categories、seasons 和 movies。
类别表
+----+----------+------------+-------------+ |编号 |姓名 |蛞蝓 | created_at | +----+----------+------------+-------------+ | 1 |它|它-2017 |一段时间 | | 2 |另一个 |另一个|一段时间 | | 3 |蜘蛛侠|蜘蛛侠 |一段时间 | | 4 |汽车 3 |汽车-3 |一段时间 | +----+----------+------------+-------------+季节表
+----+----------+------------+-------------+ |编号 |编号 |category_id | created_at | +----+----------+------------+-------------+ | 1 | 1 | 2 |一段时间 | | 2 | 2 | 2 |一段时间 | | 3 | 1 | 3 |一段时间 | | 4 | 3 | 1 |一段时间 | +----+----------+------------+-------------+电影台
+----+----------+------------+-------------+ |编号 |姓名 |蛞蝓 |季节ID | +----+----------+------------+-------------+ | 1 |飞行员 |飞行员 | 1 | | 2 |第二 |第二 | 1 | | 3 |第三|第三| 1 | | 4 |第四|第四| 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 季,但我只想显示当前类别的季节。
【问题讨论】:
-
试试这个
$seasons = Season::with('movies')->where('number', $season_number)->where('category_id', $categories->id)->get();!! -
@Maraboc,它给了我一个错误Property [id] does not exist on this collection instance.
-
试试这个
$category = Category::where('slug', $category_slug)->first(); $seasons = Season::with('movies')->where('number', $season_number)->where('category_id', $category->id)->get(); return view('routes.getMovies', compact('category', 'seasons')); -
效果更好...,但这是当前类别中所有季节的电影。例如。电影汽车有 3 个季节,有多个剧集,这一集将在 汽车 类别的所有季节中播放。
-
我不这么认为,因为我们有
->where('number', $season_number)->where('category_id', $category->id)它在哪里和哪里!!$season_number的值是多少?