通过中间件参数laravel 5.1实现用户和角色表的关系

通过中间件参数laravel 5.1实现用户和角色表的关系,laravel,laravel-5.1,Laravel,Laravel 5.1,我正在使用Laravel5.1开发一个用户管理网站 不允许用户注册,但他们可以查看自己的配置文件。注册只能由管理员完成,所以我有两种类型的用户(管理员和普通用户) 我将遵循本教程: 在我接触到用户模型之前,一切都正常 用户表: public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id');

我正在使用Laravel5.1开发一个用户管理网站

不允许用户注册,但他们可以查看自己的配置文件。注册只能由管理员完成,所以我有两种类型的用户(管理员和普通用户)

我将遵循本教程: 在我接触到用户模型之前,一切都正常

用户表:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->softDeletes();
            $table->integer('role_id')->unsigned();
            $table->timestamps();
        });
public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('role_name');
            //
        });
    }
    public function role(){
        return $this->belongsTo('App\Role','role_id');
    }

    public function hasRole($title){
        $user_role = $this->role();
        if(!is_null($user_role)){
            $user_role = $user_role->role_name; //here is the problem with role_name

        }
        return ($user_role===$title)?true:false;
    }
角色表:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->softDeletes();
            $table->integer('role_id')->unsigned();
            $table->timestamps();
        });
public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('role_name');
            //
        });
    }
    public function role(){
        return $this->belongsTo('App\Role','role_id');
    }

    public function hasRole($title){
        $user_role = $this->role();
        if(!is_null($user_role)){
            $user_role = $user_role->role_name; //here is the problem with role_name

        }
        return ($user_role===$title)?true:false;
    }
榜样:

class Role extends Model
{
    protected $table = 'roles';
    protected $fillable = ['role_name'];

    public function users()
    {
        return $this->hasMany('App\User');
    }
用户型号:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->softDeletes();
            $table->integer('role_id')->unsigned();
            $table->timestamps();
        });
public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('role_name');
            //
        });
    }
    public function role(){
        return $this->belongsTo('App\Role','role_id');
    }

    public function hasRole($title){
        $user_role = $this->role();
        if(!is_null($user_role)){
            $user_role = $user_role->role_name; //here is the problem with role_name

        }
        return ($user_role===$title)?true:false;
    }
在PHPStorm中,角色名称以黄色突出显示,并显示

在类中找不到字段“role\u name” \Illumb\Database\Eloquent\Relations\BelongsTo less。。。(Ctrl+F1) 在subject类中找不到引用的字段。注:支票不适用 对类型为“stdClass”或派生的对象执行

我创建了3个中间件更新、创建和删除,它们都具有相同的句柄功能:

public function handle($request, Closure $next, $Admin)
    {
        $User = $request->user();
        return ($User->hasRole($Admin))?$next($request):response(view('errors.401'),401);

    }
路由文件:

Route::get('/users','UserController@index');
Route::post('/users/create',['middleware'=>'create:Admin','uses'=>'UserController@store']);
Route::patch('/users/{id}',['middleware'=>'update:Admin','uses'=>'UserController@update']);
Route::delete('/users/{id}',['middleware'=>'delete:Admin','uses'=>'UserController@destroy']);
每当我打开“创建”页面时,都会出现以下错误:

“C:\wamp\www\laravelU\project-Copy5\app\User.php中的错误异常 第42行:未定义的属性: 照亮\数据库\雄辩\集合::$role\u name“

我已经处理这个代码三个晚上了,我需要你的帮助。
如果没有更简单的方法来实现我的目标而不使用软件包

尝试
$user\u role->role\u name
时出现错误的原因是,从技术上讲,您正在
BelongsTo
类中查找属性
role\u name

当尝试访问雄辩模型的关系时,您可以将其用作动态属性,即

$user_role = $this->role;

因此,您可以将hasRole方法更改为:

public function hasRole($title){

    if(is_null($this->role)){
        return false;
    }

    return $this->role->role_name === $title;
}

应用程序抛出
方法NotAllowedHttpException
的原因是,您试图使用
GET
请求访问
POST
路由。(无论何时使用web浏览器导航到页面,都将是一个
GET
请求)

您应该做的是将
Route::post('create')…
更改为
Route::get('create')…
ad添加类似于
Route::post('store')
的内容以提交给

看看

其次,如果只是检查用户是否是管理员,则不需要有3个不同的中间件类

最后,看一下(包括
授权
部分)

希望这有帮助!

谢谢罗斯! 以下是我编辑后的路线:

Route::get('/users','UserController@index');
Route::get('/users/create',['middleware'=>'create:Admin','uses'=>'UserController@create']);
Route::post('/users/create',['middleware'=>'create:Admin','uses'=>'UserController@store']);
Route::patch('/users/{id}',['middleware'=>'update:Admin','uses'=>'UserController@update']);
Route::delete('/users/{id}',['middleware'=>'delete:Admin','uses'=>'UserController@destroy']);
打开浏览器并尝试访问“创建”页面时,出现以下错误:

“C:\wamp\www\laravelU\project-Copy5\app\User.php中的错误异常 第42行:未定义的属性: 照亮\数据库\雄辩\集合::$role\u name“


将来,我会再添加一个角色,比如经理,他不能删除用户。所以我觉得使用多个中间件更容易,但是$role\u name让我很生气!

这在技术上是一个不同的问题,如果你编辑当前问题(而不是答案),我应该要求你创建一个新问题并添加包含
app\User.php
42行的方法,我可以帮助您。第42行是用户模型:$User\u role=$User\u role->role\u name;//这里是role\u name的问题