Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined variable by using eloquent relationships

Here is the error returned by Laravel :

ErrorException compact(): Undefined variable $lists

I am trying to join multiples tables like :
Tops has many Lists and Lists has many Medias

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have this code in my TopController :

 $tops = Top::with(
                    'user:id,name,email',
                    'lists:id',
                    'categories:title,slug'
                )->paginate(20); 

I have no problem for retrieve user and categories datas but the lists variable seems to be undefined and I do not understand why.

Here is the Liste.php Model :

 public function user()
    {
        return $this->belongsTo(User::class);
    }


      public function tops()
    {
        return $this->belongsToMany(Top::class);
    }



      public function medias()
    {
        return $this->belongsToMany(Medias::class);
    }

Thank you for your help.

https://pastebin.com/Anv4AhVe

>Solution :

you just need to define a $lists variable in your index method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading