在空Laravel上调用成员函数role() [英] Call to a member function roles() on null Laravel

查看:85
本文介绍了在空Laravel上调用成员函数role()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel中使用户角色起作用,并且一切正常,并且从无处出错:调用成员函数role()或null.

I'm making user roles function in Laravel and everything has worked fine and from nowhere I have error: call to a member function roles() or null.

这是我发送请求的html文件:

This is my html from where I'm sending request:

<td><input type="checkbox" {{ $user->hasRole('User') ? 'checked' : '' }} name="role_user"></td>
<td><input type="checkbox" {{ $user->hasRole('Author') ? 'checked' : '' }} name="role_author"></td>
<td><input type="checkbox" {{ $user->hasRole('Admin') ? 'checked' : '' }} name="role_admin"></td>

然后是在模型用户和模型角色之间建立关系的函数,以及在检查角色时使用的函数:

Then there are function where I'm making relationship between model User and model Role, and functions where I'm checking the roles:

 public function roles(){
        return $this->belongsToMany('App\Role','role_user','user_id','role_id');
    }

  public function hasAnyRole($roles){
        if(is_array($roles)){
            foreach ($roles as $role){
                if($this->hasRole($role)){
                    return true;
                }
            }
        }else{
            if($this->hasRole($roles)){
                return false;
            }
        }
        return false;
    }


    public function hasRole($role){
        if($this->roles->where('name',$role)->first()){
            return true;
        }
        return false;
    }

最后是我要担任新角色的功能:

And in the end the function where I'm assing new roles:

public function postAdminAssignRoles(Request $request)
    {
        $user = User::where('email', $request['email'])->first();
        $user->roles()->detach();
        if ($request['role_user']) {
            $user->roles()->attach(Role::where('name', 'User')->first());
        }
        if ($request['role_author']) {
            $user->roles()->attach(Role::where('name', 'Author')->first());
        }
        if ($request['role_admin']) {
            $user->roles()->attach(Role::where('name', 'Admin')->first());
        }
        return redirect()->back();
    }

我不知道为什么现在不能正常工作,因为到目前为止一切都很好.有什么想法吗?

I have no idea why this is not working now, because everything was working fine till now. Any ideas?

推荐答案

我猜想postAdminAssignRoles方法中发生了错误.

I guess error occurs in postAdminAssignRoles method.

可能您试图获取电子邮件的用户在数据库中不存在,因此调用first()方法将返回null.

Probably user with an email that you try to fetch does not exist in database and thus calling first() method returns null.

您可能想使用firstOrFail()来清楚地看到该电子邮件无效.或者,在使用first()提取模型后,添加if (! $user) { ... }并执行任何操作,例如重定向,以防您不想显示错误.

You might want to use firstOrFail() instead to clearly see that email is invalid. Or after fetching model with first() add if (! $user) { ... } and do whatever, like redirect, in case you don't want to display an error.

这篇关于在空Laravel上调用成员函数role()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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