Skip to content
Advertisement

Eloquent relationship returns nothing

I am trying to make a system that allow users to create playlists which add on it movies there favorites movies or songs.

I named the main model “Top”. I also have models : User, Category, Liste_top (this name is bad but it make de junctions beetween a Top and a Movie/Song. A better name, I guess, would be something like “top_media”.

So on my view I can retrieve the User datas and the Categories datas, but the Liste_top returns an empty collection, I do no understand why.

Here is my Lise_top Model :

JavaScript

The request on my TopController.php :

JavaScript

And the function on my Top Model :

JavaScript

A dd() returns this :

JavaScript

As you can see, I retrieve the User datas, the categories datas, but none from the List_top, that are just a intermediate table that relies a top from all the medias the user liked.

I would like to retrieve the datas from the medias table to do something like on my view :

JavaScript

Advertisement

Answer

You need to also include the related key in the query i.e. top_id:

JavaScript

Without this, Eloquent has not way of mapping the relationship.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement