How can I check if the user has a role for one to one relationship?

ToxifiedHashkey

I have an application with User and Role model and a one to one relationship between them. How can I check if the user has a role. I am getting an SQLSTATE[42S22] error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.user_id' in 'where clause' (SQL: select * from roles where roles.user_id = 1 and roles.user_id is not null and name = client limit 1)

  • Database Structure
Users
---------------
* id
* name
* email
* password
* role_id
* created_at

Roles
---------------
 * id
 * name
 * slug
 * created_at
  • App/Models/User
public function role() 
{
    return $this->hasOne(Role::class);
}

public function hasRole(string $role) 
{
    if ($this->role()->where('name', $role)->first()) {
        return true;
    }
    return false;
}
  • App/Models/Role
public function users() 
{
    return $this->belongsTo(User::class);
}
Felippe Duarte

You need to invert the relation. User has 1 role, Role has many Users

Role:

public function users() 
{
    return $this->hasMany(User::class);
}

User:

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

It's a One To Many relation. See the docs: https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I check if a user has one role exactly in symfony2?

How can I check that an object has at least one record?

How can I check if an entry in tkinter has one or more zeros

How can you check if a user has a particular role in a particular server

How do I check if a user has a role in discord.py?

In Laravel Eloquent, how to check for multiple relationship existence in one "has"?

How do I check in AWS if a user has two access keys and how can I find out date when one of them has been disabled?

How can I check that a list has one and only one truthy value?

Can I manage a has_one relationship with Gridfield (or similar) in Silverstripe?

How can I enable X.509 mutual authentification for only one user role in spring?

How can I route/reference one to one relationship in Ruby on Rails

How can I generate a route for a form helper with a has_one relationship?

how can I count a record that has a one to many relationship, based on importance of an attribute

How can I display all jobs and every job has it own company in one to many relationship in laravel?

How do I associate a model with a "has one" relationship on an GraphQL interface

Discord.js-commando how would I check if a mentioned user has a role, like a muted role for example?

How to check if a user has a specific role in Meteor

How to check is an user has a role on discord

How to check whether user has any role?

How to check if user has different role in mvc

Django - How to set ForeignKey to a model that itself has a one-to-one relationship to User?

Thymeleaf: Field error when applying a role to a user (One to Many relationship)

If the user has multiple roles, how to let the user choose one role to access to the application?

How can I check that the PHP multi-dimentional array has at least one value?

How can I check, if a table has certain entries and add one, if it doesn't?

How can I check which list has the least copies of one element?

How can I check if at least one ToggleButton has been Checked in a row of Buttons?

How can I check whether one's current User Points are negative with the Rules module?

Can I check what specific role a user has, from a list of roles?