Laravel package to extend your Laravel application with a simple admin permission functionality, with:
- Migration file to set a user as admin
- Middleware
- Blade directive
- Artisan command to see who is an admin
- Option to set user with ID 1 as super admin
-
composer require mvd81/laravel-is-admin -
Run
php artisan migrateto create the 'is_admin' column in theuserstable -
Import the trait in the User model
use Mvd81\LaravelIsAdmin\Traits\isAdmin;- Use the trait in the User model
class User extends Authenticatable
{
use isAdmin;
...- Add
is_adminto the$fillablearray in the User model
You can set a 'normal' user as admin by setting the database column is_admin to 1 , in database table users.
Or in the code
$user = User::find(ID);
$user->makeAdmin(); $user = User::find(ID);
$user->undoAdmin(); It is possible to use user with ID 1 as admin without setting the 'is_admin' column to 1.
First you need to publish the config file.
php artisan vendor:publish- Choose the option: Provider: Mvd81\LaravelIsAdmin\LaravelIsAdminServiceProvider
Now in config/is_admin.php set 'use_super_admin' to true.
'use_super_admin' => true,There is a IsAdmin middleware to use in your routes.
Example:
Route::get('admin-page')->middleware('IsAdmin'); Partial template/layout in your Blade view files only for admins?
You can use this Blade directive
@isAdmin()
I am an admin
@endisAdminYou can enter an artisan command to see who is an admin.
php artisan user:who-is-admincomposer remove mvd81/laravel-is-admin- Remove the config file
config/is_admin.php - Remove the database
is_admincolumn in tableusers - If you used the blade
@isAdmin()directive, remove them - Remove the
is_adminmiddleware from your routes