Php 查询列在另一个表中的位置-laravel

Php 查询列在另一个表中的位置-laravel,php,mysql,laravel,Php,Mysql,Laravel,我有三个表,分别是类别、季节和电影 类别表 +----+---------+------------+-------------+ | id | name | slug | created_at | +----+---------+------------+-------------+ | 1 | It | it-2017 | SOME TIME | | 2 | Another | another | SOME TIME | | 3 |Sp

我有三个表,分别是类别季节电影

类别表

+----+---------+------------+-------------+ | 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 | +----+---------+------------+-------------+ 和我的视图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季类似,但我只想显示当前类别的第1季。首先,请确保在您的模型上设置了正确的关系

电影模型

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

public function category()
{
    return $this->belongsTo(Category::class, 'category_id');
}
然后,以下将获得所有具有特定季号和特定类别slug的电影

$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
试试这个:

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

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

试试这个
$seasures=seasure::with('movies')->where('number',$seasure\u number)->where('category\u id',$categories->id)->get()@Maraboc,它给了我一个错误属性[id]在此集合实例上不存在。因此请尝试此
$category=category::where('slug',$category\u slug)->first()$seasons=seasons::with('movies')->where('number',$seasons_number)->where('category_id',$category->id)->get();返回视图('routes.getMovies',compact('category','seasures')效果更好…,但这是当前类别的四季放映电影。电影《汽车》有三季,有多个情节,这一情节将在《汽车类别》的所有季节都会出现。我不这么认为,因为我们有
->where('number',$seasure\u number)->where('category\u id',$category->id)
它是where和where!!
$seasure\u number
的值是多少?
$categories = Category::where('slug', $category_slug)->first()->id;

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