Hi guys, i am trying to get all seller type of users using eloquent ORM. I have three tables users, roles and user_role. User table contains standard user info name, email, password, the roles table has the list of the roles that can be assigned to a user, and user_role has both id of an user and role to as foreign key. The problem is now i am trying to get all seller type of users using this query.
$users = User::with('userRole.role')->where('machine_name', 'seller')->paginate(10);
But it returns error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'userRole.role.machine_name' in 'where clause' (SQL: select * from `users` where `userRole`.`role`.`machine_name` = seller)`
My User model looks like this
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'type',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'is_admin',
];
public function userRole(){
return $this->hasOne(\App\UserRole::class);
}
}
My UserRole Model Looks like this
class UserRole extends Model
{
protected $table = 'user_role';
public function user(){
return $this->belongsTo(\App\User::class);
}
public function role(){
return $this->belongsTo(\App\Role::class);
}
}
My Roles Model looks like this
class Role extends Model
{
public $timestamps = false;
public function userRole(){
return $this->hasMany(\App\UserRole::class);
}
}