【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.user_id' in 'where clause'SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'roles.user_id'
【发布时间】:2018-01-13 09:42:19
【问题描述】:

我在尝试从“用户”附加或检索“角色”时遇到以下错误。

SQLSTATE[42S22]:找不到列:1054 未知列 'roles.user_id' 在“where 子句”中(SQL:select * from roles where roles.user_id = 1 和 roles.user_id 不为空限制 1)

表格

public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50);
            $table->timestamps();
        });
    }

角色.php

class Role extends Model
{
    protected $fillable = ['name'];
    public $timestamps = false;

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

用户.php

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'role_id', 'username', 'firstname', 'lastname', 'active', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

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

    private function checkIfUserHasRole($need_role)
    {
        return (strtolower($need_role) == strtolower($this->role->name)) ? true : null;
        return $need_role;
    }

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

中间件/CheckRole.php

class CheckRole
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $roles = $this->getRequiredRoleForRoute($request->route());
        if ($request->user()->hasRole($roles) || !$roles){
            return $next($request);
        }
        return redirect()->route('noPermissions');
    }

    private function getRequiredRoleForRoute($route)
    {
        $actions = $route->getAction();
        return isset($actions['roles']) ? $actions['roles'] : null;
    }
}

web.php

Route::get('/', ['as' => '/', 'uses' => 'LoginController@getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@postLogin']);

Route::group(['middleware' => ['authenticates', 'checkrole']], function (){
    Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController@getLogout']);
    Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@dashboard']);
});

【问题讨论】:

  • 请附上您的角色表结构。
  • ٌ什么意思?
  • 堆栈跟踪是否提示(在您的代码中)触发错误的位置?
  • 尝试使用本教程在 Laravel 中进行基于角色的身份验证5balloons.info/…

标签: php laravel-5.5


【解决方案1】:

User 类中,您使用了错误的关系方法。尝试改变

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

进入

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

正如the documentation 所说。 hasOne 认为有问题的 roles.user_id 存在。

【讨论】:

  • 为什么用户不能拥有多个角色?
  • 问题中的实现表明每个用户都只有一个角色。如果您需要用户拥有更多角色,您应该尝试多对多关系,请参阅the documentation
猜你喜欢
  • 2021-03-29
  • 2023-04-07
  • 2019-08-18
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多