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');
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With