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

时间:2016-01-03 13:25:52

标签: laravel laravel-5.1

我正在使用laravel 5.1开发用户管理网站。

不允许用户注册,但他们可以查看自己的个人资料。注册只能由管理员完成,因此我有两种类型的用户(管理员和普通用户)。

我正在关注本教程: https://www.youtube.com/watch?v=8AQLUZ0X9ME 一切都很好,直到我达到用户模型

用户表:

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');
            //
        });
    }

角色模型:

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

    public function users()
    {
        return $this->hasMany('App\User');
    }

用户模型:

    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_name以黄色突出显示,并显示

  

在课堂上找不到字段'role_name'   \ Illuminate \ Database \ Eloquent \ Relations \ BelongsTo less ...(Ctrl + F1)   在主题类中找不到引用的字段。注意:检查不是   对“stdClass”或派生类型的对象执行。

我创建了3个中间件Update,Create和Delete,它们都具有相同的句柄功能:

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中的ErrorException - Copy5 \ app \ User.php   第42行:未定义的属性:   照亮\数据库\雄辩\收藏:: $ ROLE_NAME“

我一直在处理此代码3晚我需要你的帮助。 如果有更简单的方法来实现我的目标而不使用包?

2 个答案:

答案 0 :(得分:0)

您在尝试$user_role->role_name时收到错误的原因是因为您在技术上寻找role_name类中的属性BelongsTo

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

$user_role = $this->role;

$user_role = $this->role()->get();

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

public function hasRole($title){

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

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

您的应用投放MethodNotAllowedHttpException的原因是因为您尝试使用POST请求访问GET路由。 (只要您使用网络浏览器导航到某个页面,它就会是GET请求。)

您应该做的是将Route::post('create' ...更改为Route::get('create'...广告,添加Route::post('store'之类的内容即可提交。

查看https://laravel.com/docs/5.1/controllers#restful-resource-controllers

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

最后,请查看https://laravel.com/docs/5.1/validation#form-request-validation(包括Authorizing部分)

希望这有帮助!

答案 1 :(得分:0)

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

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中的ErrorException - Copy5 \ app \ User.php   第42行:未定义的属性:   照亮\数据库\雄辩\收藏:: $ ROLE_NAME“

将来,我将添加一个像管理员那样无法删除用户的角色。所以我觉得使用多个中间件更容易,但是$ role_name正在惹恼我!

相关问题