Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make user admin in laravel

Tags:

php

laravel

I want

I'm making a project in laravel 5.7 and I need to make a user admin and I have no separate form for this and I have only 1 migration as users, how can I implement that one can become admin?

I did

I have made a condition in user migration that if user have id == 1 then update is_admin = 1 but there's a flaw that if the I have much traffic on my site, then if I run php artisan migrate:refresh and after refreshing the database, if a user will register before registering me then he will become admin, that's what I don't need.


2 Answers

To continue with Ali Ozen's answer, after following those steps... Create a seeder using php artisan make:seeder UserSeeder

in seeder file, add below

use App\User;

User::create(['name' => 'Admin', 'email' => '[email protected]', 'password' => bcrypt('password'), 'is_admin' => 1]);

Add UserSeeder::class to DatabaseSeeder.php

Then run the migration using

php artisan migrate --seed

To refresh run

php artisan migrate:refresh --seed
like image 83
Arwin Pereira Avatar answered Sep 19 '25 19:09

Arwin Pereira


php artisan make:migration add_is_admin_to_users_table --table="users"

after that open the newest migration file. In the function up function

Schema::table('users', function (Blueprint $table) {
        $table->boolean('is_admin')->default(0);
    });

Write this. Then run the php artisan migrate. Thats it. You don't need the refresh.

like image 24
Ali Özen Avatar answered Sep 19 '25 19:09

Ali Özen