简体   繁体   English

laravel 三对多关系

[英]laravel three one to many relations

I learn and I want to create in laravel something like a kind of movie library.我学习并想在 laravel 中创建类似电影库的东西。 I have three tables:我有三张表:

Series, series_seasons, series_espisodes系列、系列_季节、系列_剧集

Series model:系列型号:

public function series_seasons()
{
    return $this->hasMany('App\SeriesSeason','series_id');
}

Seasons model:季节模型:

public function series_episodes()
    {
        return $this->hasManyThrough('App\SeriesEpisode','App\SeriesSeason','series_id','season_id');
    }

 public function series()
    {
        return $this->belongsTo('App\Series','series_id');
    }

(initially instead 'hasManyThrough' I had 'hasMany') (最初改为 'hasManyThrough' 我有 'hasMany')

episodes model:情节模式:

public function series_season()
{
    return $this->belongsTo('App\SeriesSeason','season_id');
}

So my question is how can I get: series->series_seasons->all episodes of a specific season ?所以我的问题是如何获得: series->series_seasons-> all episodes of a specific season ?

You can add field season_id in your series_espisodes table.您可以在series_espisodes表中添加字段season_id Then you can retrieve all of them like this:然后你可以像这样检索所有这些:

$allEpisodes = Episodes::where('season_id', $seasonID);

You can add series_id field as well, and then you can retrieve all of it like this:您也可以添加series_id字段,然后您可以像这样检索所有内容:

$allEpisodes = Episodes::where(['season_id'=> $seasonID, 'series_id => $seriesID]);

This would be the easiest way of doing this, without making any complicated logic behind it.这将是最简单的方法,无需在其背后制定任何复杂的逻辑。 And you have everything in there.你拥有一切。 If you create it right, you can skip seasons table, because it's only a number.如果创建正确,则可以跳过seasons表,因为它只是一个数字。 So you can have two tables, one for the series and one for the episodes.所以你可以有两张桌子,一张用于剧集,一张用于剧集。 Simple and effective.简单有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM