Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ryank30's avatar

users and roles eloquent table relationship

Hi,

I am building a relationship between users table and roles table. I have created User class and Role Class for those tables.

In User class:

public function roles()
{
    return $this->belongsTo('App\Role');    
}

In Role class:

public function users(){
    return $this->hasMany('App\User');
}

When I try to run a function in User class page below, it says non-object. (please see below)

public function isATeamManager()
{
    $roles = Auth::user()->roles()->first();
    $role_name = $roles->role_name;
    if($role_name == "manager")
    {
        return true;
    }else{
        return false;
    }
}

Could you tell me what's wrong with my coding?

Thanks in advance.

0 likes
13 replies
mstnorris's avatar

You need to remove the parenthesis

public function isATeamManager()
{
    $roles = Auth::user()->roles->first();
    $role_name = $roles->role_name;
    if($role_name == "manager")
    {
        return true;
    }else{
        return false;
    }
}
ryank30's avatar

Hi mstnorris,

Thanks for your reply. Could you tell me specific spot please? I am not sure which parenthesis you are talking about.

mstnorris's avatar

@ryank30 are you sure your Role name is called role_name (in the database that is) ?

What is the exact error you are getting?

ryank30's avatar

I have a column called "role_name" in "roles" table.

ryank30's avatar

I am using a middleware called "manager". In that middleware class, I put a line of code inside "handle" function. Please see below:

public function handle($request, Closure $next)
{
    if(!$request->user()->isATeamManager())
    {
        return redirect('articles');
    }

    return $next($request);
}

The function "isATeamManager" is located in User class page.

ryank30's avatar

Inside the function isATeamManager, I ran a test code and got a result of null. Please see below.

public function isATeamManager()
{
    $roles = Auth::user()->roles;
    dd($roles); // I am getting null here.
    $role_name = $roles->role_name;
    if($role_name == "manager")
    {
        return true;
    }else{
        return false;
    }
}
mstnorris's avatar

In your routes file can you set up something like this and tell me if you get anything

Route::get('check', function() {
$roles = Auth::user()->roles;
    dd($roles);
}
ryank30's avatar

I have added "use App\Route" and added your code inside the function "isATeamManager". Please see below.

public function isATeamManager()
{
    //$roles = Auth::user()->roles;
    //$roles = \App\Role::all()->first();
    Route::get('check', function() {
        $roles = Auth::user()->roles;
        dd($roles);
    });
    }

I have the error message like below. Class 'App\Route' not found

mstnorris's avatar

That's not what I meant.

In your routes.php file add the following

Route::get('check', function() {
$roles = Auth::user()->roles;
    dd($roles);
}

Then go to the url http://yourdomain.com/check and tell me what the output is (if any)

ryank30's avatar

Is it necessary to set a foreign key for "role_id" in users table? I was trying to run migration with the code inside up function of creating a users table but I couldn't run it. Please see below.

        $table->foreign('role_id')
            ->references('id')
            ->on('roles');

As far as I understand, the relationship between users and roles should work through "role_id" in users table regardless of the foreign key setting in the database.

taijuten's avatar

Foreign key shouldn't be required.

When you're doing this check in your url.com/check route, you do have a user logged in, right? If there is no user authed, then it will return null.

do a

dd(Auth::user())

to check

Please or to participate in this conversation.