MythicCoder's avatar

Nested relationship returning null

For a project of mine I'm trying to set up an API through Lumen. In this project I'm creating a website for a cinema. I chose Lumen as way of gettting an understanding of something new.

The database is structured as follows:

Screenshot_2

The relationships are: A movie can have many shows, and a show has one room

To get this data from the database through Lumen I have tried the following:

MovieController controller

public function one($id) {
    return response()->json(Movie::where('id', '=',$id)->with(['types','shows.room'])->first(), 200);
}

Movie model

public function types() {
    return $this->hasMany('App\Types', 'movie_id');
}

public function shows() {
    return $this->hasMany('App\Show', 'movie_id');
}

Show model

public function movie() {
    return $this->belongsTo('App\Movie');
}

public function room() {
    return $this->hasOne('App\Room', 'id', 'room_id');
}

Room model

public function show() {
    return $this->belongsTo('App\Show', 'room_id', 'id');
}

With a lot of googling, and a lot of tries, I have come to this point where it still does the same as it did when I tried it without googling it. It returns everything BUT the room data properly. The room data remains null

Returned json

{
    "id": 1,
    "title": "Fast & Furious",
    "desc": "Geregisseerd door James Wan met Vin Diesel, Jason Statham en Lucas Black\nNadat Dominic Toretto lucht krijgt van Han's dood, reist hij met zijn crew naar Tokyo, waar ze Sean Boswell ontmoeten, de driftkoning en een vriend van Han. Ze vormen een team met Sean en enkele nieuwe vrienden en gaan erop uit om hun gevallen vriend te wreken. Hierbij stuitten ze op de man die Han heeft vermoord, Ian Shaw, de oudere broer van Owen Shaw en leider van een geheime en illegale straatrace-organisatie.",
    "genre": "Actie",
    "duration": 122,
    "age": "16",
    "price": 9.75,
    "status": "upcoming",
    "poster": "https://image.tmdb.org/t/p/original/b9gTJKLdSbwcQRKzmqMq3dMfRwI.jpg",
    "backdrop": "https://image.tmdb.org/t/p/original/qjfE7SkPXpqFs8FX8rIaG6eO2aK.jpg",
    "created_at": "2018-06-19 14:18:57",
    "updated_at": "2018-06-19 14:18:57",
    "types": [
        {
            "type": "IMAX",
            "created_at": "2018-06-19 14:18:57",
            "updated_at": "2018-06-19 14:18:57"
        },
        {
            "type": "4DX",
            "created_at": "2018-06-19 14:18:57",
            "updated_at": "2018-06-19 14:18:57"
        }
    ],
    "shows": [
        {
            "id": 1,
            "room_id": 1,
            "movie_id": 1,
            "date": "2015-04-16",
            "time": "16:30:00",
            "created_at": "2018-06-19 14:18:57",
            "updated_at": "2018-06-19 14:18:57",
            "room": null
        },
        {
            "id": 2,
            "room_id": 1,
            "movie_id": 1,
            "date": "2015-04-16",
            "time": "20:30:00",
            "created_at": "2018-06-19 14:18:57",
            "updated_at": "2018-06-19 14:18:57",
            "room": null
        }
    ]
}

I hope you can help me

0 likes
4 replies
Cronix's avatar

Try switching id and room_id, or remove them. I don't think you need to specify them since you're db is properly constructed.

return $this->hasOne('App\Room', 'id', 'room_id');
MythicCoder's avatar

@Cronix When I remove the key parameters, it starts searching for shows_id on the rooms table resulting in the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rooms.shows_id' in 'where clause'

Besides this, flipping the keys results in the room still returning null

Dalma's avatar

I had a similar issue a few weeks ago and in my case it turned out to be a data issue, the id I was looking for was missing from my table. This may not apply to you but worth a double check.

Snapey's avatar

Show belongs to room since it has room_id column

Please or to participate in this conversation.