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

Consts Laravel Package

colbeh/consts

Tiny Laravel/PHP helper for defining and accessing class constants as “const sets.” Simplifies organizing enums-like values, retrieving constant lists, and keeping shared values centralized in a clean, reusable API.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require colbeh/consts
    

    Publish the configuration (if needed):

    php artisan vendor:publish --provider="Colbeh\Consts\ConstsServiceProvider"
    
  2. First Use Case Generate constants for a database table (e.g., users):

    php artisan consts:generate users
    

    This creates a UsersConst class in app/Consts/ with column names as constants (e.g., UsersConst::NAME).

  3. Where to Look First

    • Generated Files: Check app/Consts/ for auto-generated classes.
    • Configuration: Review config/consts.php for customization (e.g., namespace, output path).
    • Artisan Commands: Run php artisan to see available commands (consts:generate, consts:clear).

Implementation Patterns

Workflows

  1. Table-Driven Development

    • Use constants for table/column names to avoid magic strings:
      $user = User::where(UsersConst::STATUS, UsersConst::ACTIVE)->first();
      
    • Update constants via php artisan consts:generate users --force.
  2. Integration with Eloquent

    • Replace hardcoded column names in queries/models:
      // Before
      User::where('status', 'active')->get();
      
      // After
      User::where(UsersConst::STATUS, UsersConst::ACTIVE)->get();
      
  3. API/Request Validation

    • Use constants in Form Requests or API Resources:
      public function rules()
      {
          return [
              UsersConst::EMAIL => 'required|email',
              UsersConst::STATUS => 'in:'.UsersConst::ACTIVE.','.UsersConst::INACTIVE,
          ];
      }
      
  4. Seeding Data

    • Reference constants in seeders for consistency:
      User::create([
          UsersConst::NAME => 'John Doe',
          UsersConst::STATUS => UsersConst::ADMIN,
      ]);
      

Advanced Patterns

  • Custom Naming: Override default naming conventions via config:
    'naming' => [
        'class' => 'UserTable',
        'constants' => 'SNAKE_CASE',
    ],
    
  • Partial Generation: Generate constants for specific columns:
    php artisan consts:generate users --columns=name,email,status
    
  • Dynamic Usage: Fetch constants at runtime (e.g., for dynamic queries):
    $column = request('column');
    $value = request('value');
    User::where($column, $value)->get(); // Replace $column with UsersConst::COLUMN if known.
    

Gotchas and Tips

Pitfalls

  1. Namespace Collisions

    • Ensure app/Consts/ is excluded from autoloading if using custom namespaces.
    • Fix: Add Consts/ to composer.json under "autoload-exclude".
  2. Database Schema Changes

    • Regenerate constants after altering tables:
      php artisan consts:generate users --force
      
    • Tip: Use migrations with --force to avoid stale constants.
  3. Reserved Words

    • Avoid column names like class, table, or PHP keywords.
    • Workaround: Use aliases in the database (e.g., user_classUsersConst::USER_CLASS).
  4. Case Sensitivity

    • Constants are generated in UPPER_SNAKE_CASE by default. Adjust in config if needed.

Debugging

  • Missing Constants: Verify the table exists and the command ran successfully.
    php artisan consts:generate users --verbose
    
  • Cache Issues: Clear cached configs if changes don’t apply:
    php artisan config:clear
    
  • IDE Autocomplete: Ensure your IDE indexes app/Consts/ (e.g., PHPStorm: File > Invalidate Caches).

Extension Points

  1. Custom Generators

    • Extend the generator by publishing and modifying the template:
      php artisan vendor:publish --tag=consts.templates
      
    • Override app/Consts/ConstGenerator.php.
  2. Post-Generation Hooks

    • Listen to the consts.generated event in EventServiceProvider:
      public function boot()
      {
          Consts::generated(function ($table, $class) {
              // Run custom logic (e.g., log, notify)
          });
      }
      
  3. Multi-Tenant Support

    • Generate tenant-specific constants by extending the command:
      php artisan consts:generate users --tenant=acme
      
    • Tip: Use config to define tenant-specific paths.

Performance

  • Avoid Over-Generating: Regenerate only changed tables to save time.
  • Use --no-backup: Skip backup files for CI/CD pipelines:
    php artisan consts:generate users --no-backup
    
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.
besmartand-pro/php-quality-config
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views
spatie/ignition-contracts