Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel dynamic property not working

I'm using the eloquent ORM in Laravel with a hasMany relationship.

when I run:

Level::find(1)->lessons()->get();

It works fine, but when I use the dynamic property like so:

Level::find(1)->lessons

It just returns results for the level instead of the lessons.

Do I need another setting somewhere?

EDIT: Here are the models:

class Level extends Eloquent {

    protected $table = 'levels';

    public function lessons()
    {
        return $this->hasMany('Lesson');
    }
}

class Lesson extends Eloquent {

    protected $table = 'lessons';

    public function level()
    {
        return $this->belongsTo('Level');
    }
}
like image 822
Rob Avatar asked Sep 10 '13 15:09

Rob


1 Answers

I just had the same problem, turns out I had a column on the table that had the same name as the relationship I set up.

Make sure you don't have a column in the model that has the same name as the relationsip method you are trying to load.

EDIT: I also noticed laravel has problems with undescores (_) in relationship names, so don't put an _ in the methodname or else it won't work.

like image 98
Ezra Avatar answered Oct 18 '22 09:10

Ezra