Laravel验证::用户()关系 [英] Laravel Auth::user() relationships

查看:189
本文介绍了Laravel验证::用户()关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得通过验证我的用户的角色关系::用户()函数。我以前做过这一点,但由于某种原因,它不工作。

I am trying to get my users role relation through the Auth::user() function. I have done this before but for some reason it is not working.

Auth::user()->role

这将返回错误试图从非对象的属性。

This returns the error trying to get property from non-object.

在我的用户模型我有这样的:

In my user model I have this:

public function role()
{
    return $this->belongsTo('vendor\package\Models\Role');
}

在我的榜样,我有:

public function user()
    {
        return $this->hasMany('vendor\package\Models\User');
    }

当我这样做,它返回我的角色的名字,所以我的关系是正确的,我认为:

When I do this it returns the name of my role, so my relations are correct I think:

User::whereEmail('test@test.be')->first()->role->name

我是什么失踪?

推荐答案

好吧,我发现了为什么它不为我工作。
事情是,我在那里谈论我的用户模型是我包的一部分,并且由于Laravel在默认安装Laravel它自己的用户模型它不工作。

Ok I found out why it wasn't working for me. The thing is that my User model where I was talking about was a part of my package, and because Laravel has it's own User model in the default Laravel installation it was not working.

您封装模式不会覆盖已经存在的模式。我通过一个特质,而不是一个模式,为我的包解决了我的问题。

Your package model does not override an already existing model. I solved my problem by making a Trait instead of a model for my package.

这篇关于Laravel验证::用户()关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆