【问题标题】:Laravel 5.3: Can not access anymore the admin dashboardLaravel 5.3:无法再访问管理仪表板
【发布时间】:2017-03-02 07:52:28
【问题描述】:

有人可以帮我解决问题吗?

我正在 Laravel 中创建一个区域,用户可以在其中根据自己的角色登录。

使用工匠命令创建身份验证,到目前为止非常简单。

然后,我首先更改了数据库中表“用户”的表名和主键。 完成此操作后,需要更新用户模型(由 artisan 命令自动生成)并让模型知道“用户”表的确切位置以及该表的主键。

protected $table = 'users';
protected $primaryKey = 'userID';

在此之后,一旦我进入浏览器并进行正常登录,它就不允许我访问管理仪表板,并且我得到提示 trying to get a property of a non-对象。

这是因为一旦像我之前所做的那样更改了表和主键,'$this' 就不再在对象上下文中了。

我怎样才能让它工作?

用户.php:

class User extends Authenticatable
{


(...) public function isAdmin(){


        if($this->roles->Role_Type  == "Admin" && $this->is_active == 1){        //this one is the line 83 where the error is


            return true;

        }


        return false;



    }




 (...)

}

【问题讨论】:

  • 你试过php artisan dump-autoload吗?
  • 我已经尝试过了,但问题仍然存在。实际使用:composer dump-autoload
  • 你应该使用 artisan comman 代替。它清除 Laravel 编译的源代码。完整的异常消息是什么?
  • 它告诉我命令未定义(命令“dump-autoload”未定义)。抱歉回复晚了
  • 抱歉,没看到您使用的是 L5.3。是的,该命令已在 L5 中删除。但仍然:完整的异常消息是什么?

标签: php database laravel model


【解决方案1】:

它是这样工作的:

去数据库并删除了在表之间建立的强关系 (FK),这对开发人员来说总是很痛苦,需要更多的代码才能将它们保存在那里。

感谢大家的帮助,下面是使用和运行良好的代码:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    //
    protected $table = 'users';
    protected $primaryKey = 'userID';

    protected $fillable = [
        'name',
        'email',
        'password',
        'roleID',
        'photoID',
        'is_active'
    ];

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



    public function role(){

        return $this->belongsTo('App\Role', 'roleID');


    }



    public function photo(){


        return $this->belongsTo('App\Photo', 'photoID');


    }




//    public function setPasswordAttribute($password){
//
//
//        if(!empty($password)){
//
//
//            $this->attributes['password'] = bcrypt($password);
//
//
//        }
//
//
//        $this->attributes['password'] = $password;
//
//
//
//
//    }



    public function isAdmin(){


        if($this->role->Role_Type  == "Admin" && $this->is_active == 1){


            return true;

        }


        return false;



    }



    public function posts(){


        return $this->hasMany('App\Post');


    }



    public function getGravatarAttribute(){


        $hash = md5(strtolower(trim($this->attributes['email']))) . "?d=mm&s=";
        return "http://www.gravatar.com/avatar/$hash";


    }





}

【讨论】:

    【解决方案2】:

    这段代码有错误

    $this->roles->Role_Type
    

    "$this->roles" 返回与该用户对应的角色,然后您选择了字段“Role_Type”。

    在您的场景中,该用户没有附加任何角色。

    所以这个 "$this->roles" 返回 null。

    所以你不能取值“Role_Type”。这会导致错误。

    您必须执行以下操作

    if($this->roles != null && $this->roles->Role_Type && $this->is_active == 1) {        
        return true;
    } else {
        return false;
    }
    

    注意:您的代码将使用一种关系。

    Class User extends Model
    {
        public function roles()
        {
            return $this->hasOne('App\Role');
        }
    }
    

    如果你想有效地使用角色和权限,试试这个包https://github.com/romanbican/roles

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 2020-01-27
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 2015-10-13
      • 2016-07-02
      • 2020-03-09
      • 2016-12-08
      相关资源
      最近更新 更多