ekyna/user
User management bundle for Laravel/PHP apps, providing a structured foundation for users, roles and authentication-related features. Designed to integrate into your project as a reusable package, helping standardize user handling across applications.
Installation
composer require ekyna/user
Publish the package configuration and migrations:
php artisan vendor:publish --provider="Ekyna\User\UserServiceProvider" --tag="config"
php artisan vendor:publish --provider="Ekyna\User\UserServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
Service Provider & Facade
Ensure the package is registered in config/app.php under providers:
Ekyna\User\UserServiceProvider::class,
Use the facade for quick access:
use Ekyna\User\Facades\User;
First Use Case: User Creation Create a user via the facade:
$user = User::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => bcrypt('secure123'),
]);
User Management
// Fetch a user
$user = User::find(1);
// Update a user
$user->update(['email' => 'new@example.com']);
// Delete a user
$user->delete();
$validated = User::validate([
'name' => 'Jane Doe',
'email' => 'invalid-email',
]);
Authentication Integration
php artisan make:auth
User model in config/auth.php:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => Ekyna\User\Models\User::class,
],
],
Custom Fields & Extensions
User model by creating a trait or subclass:
namespace App\Models;
use Ekyna\User\Models\User as BaseUser;
class User extends BaseUser
{
protected $casts = [
'is_admin' => 'boolean',
];
}
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->default(false);
});
API Resource
php artisan make:resource UserResource --model=Ekyna\User\Models\User
public function toArray($user)
{
return [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
];
}
Migration Conflicts
users table, avoid running the package’s migrations directly. Instead, manually merge the required fields (name, email, password, etc.) into your existing table.Facade vs. Service Container
// Bad: Facade
$user = User::find(1);
// Good: Dependency Injection
public function __construct(Ekyna\User\Models\User $userModel) {
$this->userModel = $userModel;
}
Password Hashing
Hash facade for password hashing. If not, manually hash passwords:
use Illuminate\Support\Facades\Hash;
$hashedPassword = Hash::make('plain-text-password');
Model Binding
User model:
Route::get('/users/{user}', function (Ekyna\User\Models\User $user) {
return $user;
});
Check for Configuration Overrides
config/user.php (if published) for any custom settings that might override defaults.Log User Events
created, updated) for debugging:
User::created(function ($user) {
Log::info('User created: ' . $user->email);
});
Database Queries
DB::enableQueryLog() to inspect queries:
$users = User::all();
dd(DB::getQueryLog());
Custom User Model
User model by binding your custom model in the service provider:
$this->app->bind(
Ekyna\User\Contracts\User::class,
App\Models\User::class
);
Policy & Authorization
php artisan make:policy UserPolicy --model=Ekyna\User\Models\User
class UserPolicy {
public function viewAny(User $user) { ... }
}
Observers
User model for automated actions:
use Ekyna\User\Models\User;
use Ekyna\User\Observers\UserObserver;
User::observe(UserObserver::class);
API Middleware
Route::middleware('auth:api')->group(function () {
Route::apiResource('users', UserController::class);
});
How can I help you explore Laravel packages today?