Skip to content

Admin Role and User

Syahril Zulkefli edited this page Aug 16, 2015 · 4 revisions

Creating Roles and Users

If you try to access your site right now, you'll be served with login form, but at the moment, you don't have an account!

To make our life easier, let's create a database seeder to do all these task for us.

php artisan make:seeder RoleAndUserSeeder

Now open the file located at /app/database/seeds/RoleAndUserSeeder.php and fill it with the codes below.

use Illuminate\Database\Seeder;

class RoleAndUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $role = Pingpong\Trusty\Role::create([
	        'name' => 'Admin',
	        'slug' => 'admin',  // MUST USE admin, DO NOT CHANGE!
	        'description' => 'The one who manage the site',
	        'created_at' => new \Carbon\Carbon(),
	        'updated_at' => new \Carbon\Carbon(),
        ]);

	    $user = Pingpong\Admin\Entities\User::create([
		    'name' => 'Site Admin',
		    'email' => 'admin@example.com',
		    'password' => 'enter your password here',
		    'created_at' => new \Carbon\Carbon(),
		    'updated_at' => new \Carbon\Carbon(),
	    ]);

	    $user->roles()->attach($role);
    }
}

Then open your app/database/seeds/DatabaseSeeder.php and add this code inside run() method.

$this->call(RoleAndUserSeeder::class);

Run this command to create the role and user.

php artisan db:seed

Now you can login to your site using the email and password declared in your seeder file.

Clone this wiki locally