Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

User Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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
    
  2. 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;
    
  3. First Use Case: User Creation Create a user via the facade:

    $user = User::create([
        'name' => 'John Doe',
        'email' => 'john@example.com',
        'password' => bcrypt('secure123'),
    ]);
    

Implementation Patterns

Core Workflows

  1. User Management

    • CRUD Operations: Use the facade or service container to interact with users:
      // Fetch a user
      $user = User::find(1);
      
      // Update a user
      $user->update(['email' => 'new@example.com']);
      
      // Delete a user
      $user->delete();
      
    • Validation: Leverage Laravel’s built-in validation or extend the package’s validation rules:
      $validated = User::validate([
          'name' => 'Jane Doe',
          'email' => 'invalid-email',
      ]);
      
  2. Authentication Integration

    • Use Laravel’s default auth scaffolding with the package’s user model:
      php artisan make:auth
      
    • Override the User model in config/auth.php:
      'providers' => [
          'users' => [
              'driver' => 'eloquent',
              'model' => Ekyna\User\Models\User::class,
          ],
      ],
      
  3. Custom Fields & Extensions

    • Extend the 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',
          ];
      }
      
    • Add custom fields to the migration:
      Schema::table('users', function (Blueprint $table) {
          $table->boolean('is_admin')->default(false);
      });
      
  4. API Resource

    • Generate an API resource for the user:
      php artisan make:resource UserResource --model=Ekyna\User\Models\User
      
    • Customize the resource to include/exclude fields:
      public function toArray($user)
      {
          return [
              'id' => $user->id,
              'name' => $user->name,
              'email' => $user->email,
          ];
      }
      

Gotchas and Tips

Common Pitfalls

  1. Migration Conflicts

    • If you’ve already created a users table, avoid running the package’s migrations directly. Instead, manually merge the required fields (name, email, password, etc.) into your existing table.
  2. Facade vs. Service Container

    • Prefer dependency injection over the facade for better testability:
      // Bad: Facade
      $user = User::find(1);
      
      // Good: Dependency Injection
      public function __construct(Ekyna\User\Models\User $userModel) {
          $this->userModel = $userModel;
      }
      
  3. Password Hashing

    • Ensure the package uses Laravel’s Hash facade for password hashing. If not, manually hash passwords:
      use Illuminate\Support\Facades\Hash;
      $hashedPassword = Hash::make('plain-text-password');
      
  4. Model Binding

    • If using Laravel’s implicit model binding, ensure the route model binding is configured for the package’s User model:
      Route::get('/users/{user}', function (Ekyna\User\Models\User $user) {
          return $user;
      });
      

Debugging Tips

  1. Check for Configuration Overrides

    • Verify config/user.php (if published) for any custom settings that might override defaults.
  2. Log User Events

    • Listen to user events (e.g., created, updated) for debugging:
      User::created(function ($user) {
          Log::info('User created: ' . $user->email);
      });
      
  3. Database Queries

    • Use Laravel Debugbar or DB::enableQueryLog() to inspect queries:
      $users = User::all();
      dd(DB::getQueryLog());
      

Extension Points

  1. Custom User Model

    • Override the default User model by binding your custom model in the service provider:
      $this->app->bind(
          Ekyna\User\Contracts\User::class,
          App\Models\User::class
      );
      
  2. Policy & Authorization

    • Attach policies to the user model for authorization:
      php artisan make:policy UserPolicy --model=Ekyna\User\Models\User
      
      class UserPolicy {
          public function viewAny(User $user) { ... }
      }
      
  3. Observers

    • Add observers to the User model for automated actions:
      use Ekyna\User\Models\User;
      use Ekyna\User\Observers\UserObserver;
      
      User::observe(UserObserver::class);
      
  4. API Middleware

    • Protect API routes with middleware targeting the user model:
      Route::middleware('auth:api')->group(function () {
          Route::apiResource('users', UserController::class);
      });
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager