I'm having an issue with eloquent whereby when I call $unit->assets
I am receiving an empty collection. But if I call $unit->assets()->get()
- I receive a collection with the correct results.
According to the documentation my relations are defined correctly, which also seems apparent given that I get correct results when calling get()
on the relationship object.
class Unit extends Model
{
protected $table = 'organisation_units';
public function assets()
{
return $this->hasMany(MediaElement::class, 'owner_id');
}
}
class MediaElement extends Model
{
protected $table = 'template_elements';
public function owner()
{
return $this->belongsTo(Unit::class, 'owner_id');
}
}
Table structure is
organisation_units
id | name
template_elements
id | owner_id | name | filename