Question: I have 2 tables: roles & users.

In users I have role\_id, and I want to check if that role has a column "access\_admin\_area" on true. If true, I am using a middleware. 

```
Gate::define('admin', function ($user) {
   return !empty($user->roles()->where('access_admin_area', true)->first());
});

```

**From User model:**

```
public function roles()
{
    return $this->hasOne(Role::class);
}

```

> 
> SQLSTATE[42703]: Undefined column: 7 ERROR: column roles.user\_id does not exist↵LINE 1: select \* from "roles" where "roles"."user\_id" = $1 and "role..
> 
> 
>

Answer: 
The error describes the issue pretty nicely here - the hasOne relationship method inside your User model expects the Role table row to have a user\_id column that specifies a foreign key referencing the user table id column.

If I was you, I'd rather use hasMany relationhip between your User and Role model in this use case, since I expect your users and roles should have a many-to-many relationship

check out the many-to-many relationship eloquent and database structure in the laravel documentation <https://laravel.com/docs/7.x/eloquent-relationships#many-to-many>
You should change **hasOne** to **belongsTo**  

then you can a simpler way to save yourselve from many where clauses in your controlleer is by creating another relationship with eg name as **rolewithadminaccess**

```
public function roles()
{
    return $this->belongsTo(Role::class);
}

public function rolewithadminaccess()
{
    return $this->roles()->where('access_admin_area', true)->limit(1);
}

```

then you can do this in your controller

```
return $user->rolewithadminaccess;

```