Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get all Movies that are playing on a MovieTheater? #27

Closed
e200 opened this issue Apr 9, 2019 · 1 comment
Closed

How to get all Movies that are playing on a MovieTheater? #27

e200 opened this issue Apr 9, 2019 · 1 comment

Comments

@e200
Copy link

e200 commented Apr 9, 2019

First of all, thanks for try to make our lifes easier! This is a very good solution. :)

Give me a little help?

I have the following eloquent entities:

  • Movie
  • MovieSession
  • MovieTheater
  • MovieTheaterRoom

where a MovieTheater hasMany MovieTheaterRooms, each MovieTheaterRoom hasMany MovieSessions and each MovieSession belongsTo a Movie.

What I'm trying, is to get all Movies that are playing today on a MovieTheater by its $movieTheaterId, but since it is a long relation, I'm unable of retrieve such collection.

This is what I'have tried:

class MovieTheater extends Model {
    use HasRelationships;

    public function movies()
    {
        return $this->hasManyDeep(Movie::class, [MovieSession::class, MovieTheaterRoom::class]);
    }
}

And when calling App\Models\MovieTheater::find(1)->movies()->get()

This is the output:

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'movie_sessions.movie_theater_id' in 'field list' (SQL: select movies.*, movie_sessions.movie_theater_id from movies inner join movie_theater_rooms on movie_theater_rooms.id = movies.movie_theater_room_id inner join movie_sessions on movie_sessions.id = movie_theater_rooms.movie_session_id where movie_sessions.movie_theater_id = 1)'

Where I'm wrong?

@staudenmeir
Copy link
Owner

staudenmeir commented Apr 9, 2019

Reverse the intermediate models and specify custom keys for the BelongsTo relationship:

class MovieTheater extends Model {
    use HasRelationships;

    public function movies()
    {
        return $this->hasManyDeep(
            Movie::class,
            [MovieTheaterRoom::class, MovieSession::class],
            [null, null, 'id'],
            [null, null, 'movie_id']
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants