dongqiongzheng0615 2013-07-02 19: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 20:29
    关注

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

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

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助