dongqiongzheng0615 2013-07-02 11:26
浏览 36
已采纳

在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 :)

  • 写回答

1条回答 默认 最新

  • dongqindan4406 2013-07-02 12:29
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部