Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bakerhasan's avatar

Eloquent Movie Series and Session Relation

my db tables

  1. items
  2. series
  3. session

relation logic:

// in Item.php

    protected $fillable = ['title', 'session_id', 'series_no', 'desc', 'image_url', 'aws_video_url'];   

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



// in Series.php

    public function items()
    {
      return $this->hasMany(Item::class, 'series_id');
    }


// in Session.php

  public function items()
  {
      return $this->hasMany('App\Item', 'session');
  }

it's okey when I fetch result from any single model.

But I want to fetch item like

  • series (Harry potter)
    • session 1

      • Harry Potter and the Philosopher's Stone (2001)
      • Harry Potter and the Chamber of Secrets (2002)
    • session 2

      • Harry Potter and the Prisoner of Azkaban (2004)
      • Harry Potter and the Goblet of Fire (2005)
    • session 3

      • Harry Potter and the Order of the Phoenix (2007)

continue ...

0 likes
7 replies
Snapey's avatar

ok, so

  • Series hasMany Session, Session belongsTo Series
  • Session hasMany Item, Item belongsTo Session

You need series_id on sessions table

You need session_id on items table

define the relationships as described above and you won't need to provide key names in your relationship (as these are often wrong)

bakerhasan's avatar

But the problem is every series owned all of session.

  • because:
  • series 1 can have session 1 to session 10
  • and
  • series 2 also can have session 1 to session 10

so how we can set the relation if you put session id in items table.

Snapey's avatar

Write it out like I did and it will be clearer

bakerhasan's avatar

like

  1. series (mission impossible)
    1. session 1
      1. mission in London
      2. mission in france
      3. blaa blaa
    2. session 2
      1. mission in Haiti
      2. mission in brazil
  2. series (harry potter)
    1. session 1
      1. Harry Potter and the Philosopher's Stone (2001)
      2. Harry Potter and the Chamber of Secrets (2002)
    2. session 2
      1. Harry Potter and the Prisoner of Azkaban (2004)
      2. Harry Potter and the Goblet of Fire (2005)

Here, Every Series has session 1 to session 10

beetv's avatar

I think I have some similar code somewhere in beetv movie app which is also one of the finest movie app that also provide tv shows, series, serials under separate categories which are actually well categorised.

jlrdw's avatar

When you say session, it it same as season?

In one I did, I just have columns for season and episode,

"some show title", "S3", "E22"

The series has a unique id. So you can easily query a series if needed.

Please or to participate in this conversation.