【问题标题】:Query where column is in another table - laravel查询列在另一个表中的位置 - laravel
【发布时间】:2017-09-25 09:24:00
【问题描述】:

我有 3 个表格,分别称为 categoriesseasonsmovies

类别表

+----+----------+------------+-------------+ |编号 |姓名 |蛞蝓 | 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 的值是多少?

标签: php mysql laravel


【解决方案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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-11
      • 2013-11-05
      • 2015-09-17
      • 2012-01-08
      • 2018-08-30
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      相关资源
      最近更新 更多