AR

custom Accessor vs normal method on the model?

Posted 2 years ago by AR

Is there any benefits of using accessors instead of a normal method when we are combining some value or we just don't want to change the actual field. For example:

// Just a normal path method to get the path to a thread to be called like $thread->path()
public function path()
{
    return "/threads/" . $this->id;
}

// Or

// Same thing but gets called with $thread->path
public function getPathAttribute()
{
        return "/threads/" . $this->id;
}

Another case would be:

public function fullName()
{
    return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
}

// OR

public function getFullNameAttribute()
{
        return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
}

Please sign in or create an account to participate in this conversation.