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

Laravel Command Laravel Package

mckenziearts/laravel-command

Dev-only Artisan generators for Laravel: quickly scaffold templated repositories, helpers, and observers. Adds make:repository and related commands to create boilerplate files, with configurable model namespaces for non-standard app structures.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require mckenziearts/laravel-command --dev
    

    For Laravel 5.4/5.3, register the service provider in AppServiceProvider.php under local environment:

    if ($this->app->environment('local')) {
        $this->app->register('Mckenziearts\LaravelCommand\LaravelCommandServiceProvider');
    }
    
  2. First Use Case: Generate a repository for a model (e.g., User):

    php artisan make:repository User
    

    This creates a UserRepository in App/Repositories/ with basic CRUD scaffolding.


Where to Look First

  • Command Reference: Focus on the three core commands:
    • make:repository → Generates repository classes (e.g., for Eloquent models).
    • make:observer → Creates observer classes with lifecycle hooks (creating, created, etc.).
    • make:helper → Scaffolds utility classes (e.g., MediaHelper for image processing).
  • Generated Files: Check the App/Repositories/, App/Observers/, and App/Helpers/ directories for templates.
  • Customization: Override default namespaces in generated files if your models/helpers live in custom paths (e.g., Modules/User/Models/User).

Implementation Patterns

Usage Patterns

  1. Repository Pattern:

    • Use make:repository to auto-generate a repository for a model (e.g., Post).
    • Extend the generated class to add custom logic:
      namespace App\Repositories;
      use App\Models\Post;
      
      class PostRepository extends \App\Repositories\PostRepository // Extend base if needed
      {
          public function publishedPosts()
          {
              return $this->model->where('published', true)->get();
          }
      }
      
    • Workflow: Register the repository in AppServiceProvider:
      $this->app->bind('App\Repositories\PostRepository', function ($app) {
          return new \App\Repositories\PostRepository(new Post());
      });
      
  2. Observer Pattern:

    • Generate an observer for a model (e.g., Order):
      php artisan make:observer Order
      
    • Implement hooks in App/Observers/OrderObserver.php:
      public function created(Order $order)
      {
          Log::info("Order {$order->id} created by {$order->user_id}");
      }
      
    • Workflow: Attach the observer to the model in a service provider:
      \App\Models\Order::observe(\App\Observers\OrderObserver::class);
      
  3. Helper Classes:

    • Use make:helper to create utility classes (e.g., StringHelper):
      php artisan make:helper StringHelper
      
    • Populate with static methods:
      namespace App\Helpers;
      class StringHelper {
          public static function slugify(string $text): string
          {
              return Str::slug($text);
          }
      }
      
    • Workflow: Autoload helpers via composer.json:
      "autoload": {
          "psr-4": {
              "App\\Helpers\\": "app/Helpers/"
          }
      }
      

Workflows

  1. Model-Centric Development:

    • Create Modelphp artisan make:model Post -m
    • Generate Repositoryphp artisan make:repository Post
    • Extend Repository → Add custom methods (e.g., findBySlug).
    • Register Binding → Bind the repository to the container.
  2. Event-Driven Logic:

    • Generate Observerphp artisan make:observer Post
    • Implement Hooks → Override creating, updating, etc.
    • Attach Observer → Register in a service provider or model’s boot() method.
  3. Controller Helper Methods:

    • Generate Helperphp artisan make:helper PostHelper
    • Add Logic → Move repetitive controller code (e.g., validation, API responses) to the helper.
    • Use in Controllers:
      use App\Helpers\PostHelper;
      PostHelper::formatForApi($post);
      

Integration Tips

  1. Namespaces:

    • If models/helpers live in custom namespaces (e.g., Modules\User\Models), manually update the use statements in generated files.
  2. Service Container:

    • Bind repositories/observers explicitly to avoid ambiguity:
      $this->app->singleton(\App\Repositories\PostRepository::class, function ($app) {
          return new \App\Repositories\PostRepository(new \App\Models\Post());
      });
      
  3. Testing:

    • Mock repositories/observers in tests:
      $this->app->instance(\App\Repositories\PostRepository::class, Mockery::mock());
      
  4. CI/CD:

    • Run generated commands in pipelines (e.g., php artisan make:repository User in a setup script).

Gotchas and Tips

Pitfalls

  1. Namespace Mismatches:

    • Generated files assume models/helpers are in App\Models/ and App/Helpers/. If your project uses custom paths (e.g., App/Modules/User/Models), manually update the use statements.
  2. Observer Registration:

    • Observers must be registered (via observe()) to trigger. Forgetting this step means hooks won’t fire.
  3. Repository Binding:

    • Repositories are not auto-bound to the container. You must register them manually in a service provider or use Laravel’s bind() method.
  4. Helper Autoloading:

    • Helpers won’t work unless their namespace is added to composer.json’s autoload section. Run composer dump-autoload after adding new helpers.
  5. Laravel Version:

    • The package was last updated in 2018 and may not support Laravel 10+. Test thoroughly or fork the package for modern Laravel versions.

Debugging

  1. Command Not Found:

    • Ensure the service provider is registered (check config/app.php or AppServiceProvider).
    • Verify the package is installed in composer.json under require-dev.
  2. Observer Hooks Not Triggering:

    • Confirm the observer is attached to the model:
      \App\Models\Post::observe(\App\Observers\PostObserver::class);
      
    • Check for typos in method names (e.g., created vs. created()).
  3. Repository Methods Not Available:

    • Ensure the repository is bound to the container and the model is injected correctly:
      $repository = app(\App\Repositories\PostRepository::class);
      
  4. Helper Methods Unrecognized:

    • Run composer dump-autoload and verify the helper’s namespace is in composer.json.

Tips

  1. Custom Templates:

    • Override the package’s stub files (located in vendor/mckenziearts/laravel-command/src/stubs/) to modify generated code. Publish them to your project:
      php artisan vendor:publish --provider="Mckenziearts\LaravelCommand\LaravelCommandServiceProvider" --tag="stubs"
      
  2. Command Extensions:

    • Extend the package’s commands by creating custom commands that reuse its logic. Example:
      namespace App\Console\Commands;
      use Illuminate\Console\Command;
      use Mckenziearts\LaravelCommand\Commands\MakeRepository;
      
      class CustomMakeRepository extends MakeRepository {
          protected $signature = 'make:custom-repo {name}';
          // Override logic here
      }
      
  3. Environment-Specific Commands:

    • Restrict commands to specific environments (e.g., local) by checking $this->app->environment() in the command’s handle() method.
  4. Performance:

    • For large-scale repositories, lazy-load models or use query scopes to avoid N+1 issues:
      public function allWithRelations()
      {
          return $this->model->with('comments', 'author')->get();
      }
      
  5. Documentation:

    • Add PHPDoc blocks to generated repositories/observers to clarify usage:
      /**
       * Get published posts with comments.
       *
       * @return \Illuminate\Database\Eloquent\Collection
       */
      public function publishedWithComments()
      
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.
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
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle