snoeren-development/laravel-admin-users
Install the package via Composer:
composer require snoeren-development/laravel-admin-users
Publish the package's configuration and migrations (if needed):
php artisan vendor:publish --provider="SnoerenDevelopment\AdminUsers\AdminUsersServiceProvider"
For Laravel 10/11/12/13, register the service provider in config/app.php under providers. The package now supports Laravel 13 (tested with latest stable). Start by defining admin users in config/admin-users.php and use the AdminUser facade or service container to interact with admin users:
use SnoerenDevelopment\AdminUsers\Facades\AdminUser;
// Create an admin user
AdminUser::create(['name' => 'Admin', 'email' => 'admin@example.com']);
// Check if current user is an admin
if (AdminUser::isAdmin()) { ... }
Admin User Management
Use the AdminUser facade or service container to manage admin users:
// Create
AdminUser::create(['name' => 'Super Admin', 'email' => 'super@example.com']);
// Find
$admin = AdminUser::find(1);
// Check permissions (if using middleware)
if (AdminUser::can('manage_users')) { ... }
Middleware Integration
Attach the AdminUserMiddleware to routes requiring admin access:
Route::middleware(['web', 'admin'])->group(function () {
// Admin-only routes
});
Policy-Based Permissions
Define policies in app/Policies/AdminUserPolicy.php:
public function canManageUsers(User $user) {
return $user->isAdmin();
}
Laravel 13 Support
app/Providers/AppServiceProvider for bootstrapping.public function register()
{
$this->app->bind(\SnoerenDevelopment\AdminUsers\Contracts\AdminUserContract::class, \SnoerenDevelopment\AdminUsers\Services\AdminUserService::class);
}
Customizing Admin Users
Extend the AdminUser model:
namespace App\Models;
use SnoerenDevelopment\AdminUsers\Models\AdminUser as BaseAdminUser;
class AdminUser extends BaseAdminUser
{
protected $casts = [
'is_super_admin' => 'boolean',
];
}
composer.json requires Laravel 13 if upgrading. Test thoroughly, as some internal changes (e.g., Illuminate\Foundation\Application) may affect package behavior.symfony/process (e.g., for background jobs or CLI commands).league/commonmark (if using Markdown parsing in admin panels).AdminUser::isAdmin() returns false unexpectedly, verify:
admin_users table exists and is populated.AdminUserMiddleware is correctly attached to routes.auth middleware, ensure AdminUserMiddleware runs after auth:
Route::middleware(['web', 'auth', 'admin'])->group(...);
Custom Guard:
Override the default guard in config/admin-users.php:
'guard' => 'admin',
Then configure the guard in config/auth.php.
Event Listeners:
Listen for AdminUserCreated, AdminUserDeleted, etc., in EventServiceProvider:
protected $listen = [
\SnoerenDevelopment\AdminUsers\Events\AdminUserCreated::class => [
\App\Listeners\LogAdminCreation::class,
],
];
API Resources:
Extend the default AdminUserResource for API responses:
namespace App\Http\Resources;
use SnoerenDevelopment\AdminUsers\Http\Resources\AdminUserResource as BaseResource;
class AdminUserResource extends BaseResource
{
public function toArray($request)
{
return array_merge(parent::toArray($request), [
'custom_field' => $this->custom_field,
]);
}
}
Testing:
Use the AdminUserFactory for testing:
use SnoerenDevelopment\AdminUsers\Database\Factories\AdminUserFactory;
AdminUserFactory::new()->create();
$admins = AdminUser::with('roles', 'permissions')->get();
if (cache()->remember('user-is-admin', now()->addHours(1), fn() => AdminUser::isAdmin())) { ... }
config/admin-users.php:
'table' => 'custom_admin_users',
'soft_deletes' => true,
Then use AdminUser::withTrashed() for queries.php artisan vendor:publish --tag=migrations to republish.spatie/laravel-permission, ensure the AdminUser model is properly linked to roles/permissions.api middleware group) or configure AdminUserMiddleware accordingly.How can I help you explore Laravel packages today?