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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog