【问题标题】:how to add an admin in laravel 5.1 auth manually from database如何从数据库手动在 laravel 5.1 auth 中添加管理员
【发布时间】:2018-02-04 19:02:41
【问题描述】:

任何人都知道如何通过 laravel 的身份验证?或从数据库或源代码添加管理员?假设您已经创建了一个管理面板和一个管理员用户,但是您丢失了数据库,现在您只想为您的面板创建一个新的管理员用户。我为管理面板使用了默认的 laravel 身份验证和中间件。这是kernel.php中添加的中间件

        'admin' => \App\Http\Middleware\AuthAdmin::class,

这里我们得到了角色控制器

 if(Auth::user()->isSuperAdmin()) {
        $objects = Role::select($this->dt_fields_db)->where('id','<>',1)->where('id','<>',3);
    } else {
        $objects = Role::select($this->dt_fields_db)->where('id','>',3);
    }

【问题讨论】:

  • 无法在网络上找到它,谁能告诉我上述控制器代码中的“”是什么意思?
  • &lt;&gt; 表示不等于

标签: php laravel


【解决方案1】:

创建一个新的 Seeder 类,创建一个用户和超级管理员角色,将角色分配给用户并永远成为超级管理员。

public class SuperAdminSeeder {
    public function run () {
        // modify to following commands fit your table structure
        $role = Role::create(['name' => 'super_admin'];
        $user = User::create(['email' => 'your@email.com', 'password' => bcrypt('secret')]);
        DB::table('role_user')->insert(['user_id' => $user->id, 'role_id' => $role->id]);
    }
}

通过命令行调用它:

php artisan db:seed --class=SuperAdminSeeder::class

【讨论】:

    【解决方案2】:

    只需运行 php artisan make:authphp artisan migrate。然后,将浏览器导航到 http://your-app.dev/register 或分配给您的应用程序的任何其他 URL。这两个命令将负责搭建整个身份验证系统。

    【讨论】:

    • 我的 laravel 是 5.1,不幸的是这个版本不支持 make:auth 命令,因为我必须更新它,这会损坏我的应用程序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多