0

How would I go about creating a dynamic instance variable on an Eloquent model in Laravel4?

For example, I'd like to access $user->full_name, which would be created by concatenating $user->first_name and $user->last_name, both of which are stored in the DB. This is incorrect, but something to the extent of:

class User extends Eloquent {

  public $full_name = $this->first_name . " " . $this->last_name;

}
4

1 回答 1

1

您必须定义一个getFullNameAttribute 访问器方法

class User extends Eloquent {

    public function getFullNameAttribute ()
    {
        return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];
    }
}
于 2013-09-13T15:07:08.923 回答