Travis B
Travis B

Reputation: 5288

Re-use relationship in model in Laravel

I am having issues with laravel 4 that did not happen with 3.

In the Permissions_Role model I have this relationship set up.

/**
 * User Relationship
 *
 * @return User
 */
 public function user()
 {
     return $this->belongsTo('User', 'user_id');
 }

Now, I want to use it later in the model to get that user's username. In laravel 3 this could be done with the following.

return ucword($this->user()->first()->username);

However, in four, it does not seem to return an object the same way and I can't seem to figure out the new syntax for it. Below is what I am trying currently.

/**
 * Get username
 *
 * @return string
 */
 public function getUsernameAttribute()
 {
     return ucwords($this->user()->first()->username);
 }

Any help on this would be greatly appreciated. Thanks :)

Upvotes: 1

Views: 163

Answers (1)

user2115377
user2115377

Reputation:

You dont have to write () anymore, its automaticly castet into a property

new: $this->user->username instead of $this->user()->username

Upvotes: 1

Related Questions