I have a model, Ability, which belongs to another model AbilityType.
<?php
class Ability extends Eloquent {
public function abilityType() {
return $this->belongsTo('AbilityType');
}
public function name() {
return $this->abilityType->name;
}
}
I can make this call in my blade template successfully:
$ability->abilityType->name
But when I make that same call in my Ability model, it throws an exception:
ErrorException Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
Do the dynamic properties differ in behavior between view and model layer? What am I missing here?
Laravel uses a special getFooAttribute
syntax to load dynamic properties:
class Ability extends Eloquent {
public function abilityType ()
{
return $this->belongsTo('AbilityType');
}
public function getNameAttribute ()
{
return $this->abilityType->name;
}
}
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