douzhangbao2187 2015-02-11 19:50
浏览 64
已采纳

Laravel - 具有存储库参数的模型中的方法?

I'm using laravel and i'm having some problems that I'm not quite sure what is the best way to do.

I have in my views Folder a file with the line:

{{ $user->permissionName() }}

The function permissionName() gets the User's $permission_id property and then it returns the name of the permission.

The problem is that I shouldn't get data from the model. I need to write this function on the Repository file. but now I have a new problem... How do I get this function to the view file?

I rely on the fact that the data on mvc should be like:

Model > Repository > Controller > View

or am I wrong?

  • 写回答

1条回答 默认 最新

  • douxiu6835 2015-02-11 19:57
    关注

    Couldn't you do this as a relationship instead of writing a function? On your User model you could have a relationship like this:

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function permissionName()
    {
        return $this->hasOne('App\PermissionName', 'permission_id');
    }
    

    And then you could still use {{ $user->permissionName }} in your view.

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路