Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation from model call by not view in Laravel 4

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?

like image 508
kaimerra Avatar asked Dec 22 '13 17:12

kaimerra


1 Answers

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;
    }

}
like image 173
Joseph Silber Avatar answered Nov 03 '22 17:11

Joseph Silber