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

Canvas Laravel Package

orchestra/canvas

Orchestra Canvas brings Laravel’s artisan make code generators to apps and packages. Use familiar make:* commands outside a Laravel install or with customizable namespaces and stubs, speeding scaffolding for controllers, models, migrations, and more.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require --dev "orchestra/canvas"
    

    For Laravel projects, the package auto-registers via Package Discovery. For standalone use, run commands via:

    vendor/bin/canvas make:model Post
    
  2. Configure Presets: Generate a canvas.yaml preset file tailored to your project:

    • For Laravel apps:
      vendor/bin/canvas preset laravel
      
    • For packages:
      vendor/bin/canvas preset package --namespace="Your\Package\Namespace"
      

    This sets default namespaces, paths, and stubs (e.g., App\Models\ for Laravel, Your\Package\Models\ for packages).

  3. First Use Case: Generate a model with migration (replaces php artisan make:model Post -m):

    vendor/bin/canvas make:model Post -m
    

    Verify the files are created in app/Models/ (Laravel) or src/Models/ (package) with correct namespaces.


Implementation Patterns

Core Workflows

  1. Laravel Integration:

    • Override Artisan Commands: Canvas replaces Laravel’s make:* commands seamlessly. Use php artisan make:model as usual—it now uses Canvas’s stubs and presets.
    • Fallback: If Canvas isn’t configured, Laravel’s defaults are used automatically.
  2. Package Development:

    • Standalone Scaffolding: Generate package classes without a Laravel install:
      vendor/bin/canvas make:job ProcessPayment --namespace="Your\Package\Jobs"
      
    • Custom Paths: Use canvas.yaml to define paths.src (e.g., src/) for non-standard directories.
  3. Team Standardization:

    • Shared Presets: Store canvas.yaml in version control to enforce consistent naming (e.g., App\Services\ for service classes).
    • Custom Stubs: Override default stubs (e.g., resources/stubs/model.stub) for project-specific templates.
  4. CI/CD Pipelines:

    • Pre-Commit Hooks: Use Canvas to auto-generate missing boilerplate (e.g., factories for new models):
      vendor/bin/canvas make:factory Post --model=Post
      
    • Monorepo Projects: Run Canvas in subdirectories with --namespace flags to generate isolated components.

Integration Tips

  • Stub Customization: Extend default stubs by copying them from vendor/orchestra/canvas/stubs/ to your project’s resources/stubs/ directory. Canvas will auto-detect and use your overrides. Example: Modify model.stub to include soft deletes by default:

    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Database\Eloquent\Model;
    
    class {{ class }} extends Model
    {
        use SoftDeletes;
        // ...
    
  • Dynamic Namespaces: Use the --namespace flag for one-off commands:

    vendor/bin/canvas make:controller API\V1\PostController --namespace="App\Http\Controllers\API\V1"
    
  • Laravel Service Provider: For Laravel projects, register the Orchestra\Canvas\LaravelServiceProvider in config/app.php to enable Artisan command overrides:

    'providers' => [
        // ...
        Orchestra\Canvas\LaravelServiceProvider::class,
    ],
    
  • Testing: Use canvas in PHPUnit tests to scaffold test doubles:

    public function testExample()
    {
        Artisan::call('canvas make:model TestModel');
        // Test logic...
    }
    

Gotchas and Tips

Pitfalls

  1. Namespace Conflicts:

    • Issue: Commands may generate classes with incorrect namespaces if canvas.yaml is misconfigured.
    • Fix: Explicitly set --namespace or verify canvas.yaml paths. Example:
      model:
        namespace: App\Models
      
    • Debug: Run with --verbose to see resolved namespaces:
      vendor/bin/canvas make:model Post --verbose
      
  2. Stub Overrides Not Loading:

    • Issue: Custom stubs in resources/stubs/ are ignored.
    • Fix: Ensure stubs match the exact filename (e.g., model.stub for make:model). Clear Composer’s cache if needed:
      composer clear-cache
      
  3. Laravel Artisan vs. Canvas CLI:

    • Issue: Commands behave differently between php artisan and vendor/bin/canvas.
    • Fix: Use composer exec canvas in Laravel projects to ensure consistency:
      composer exec canvas make:migration create_posts_table --create
      
  4. Migration Paths:

    • Issue: Migrations are created in the wrong directory (e.g., database/migrations/ vs. src/Migrations/).
    • Fix: Configure migration.path in canvas.yaml:
      migration:
        path: src/Migrations
      
  5. PHP Version Mismatches:

    • Issue: Canvas fails with PHP 8.5+ due to deprecated syntax in stubs.
    • Fix: Update to Canvas v10.1.0+ (PHP 8.5 compatible) or pin to a stable version:
      composer require "orchestra/canvas:^10.1"
      

Debugging Tips

  • Dry Runs: Use --dry-run to preview generated files without writing them:
    vendor/bin/canvas make:model Post --dry-run
    
  • Stub Inspection: Dump stub content to debug templates:
    vendor/bin/canvas stub:dump model
    
  • Command Help: Get detailed usage for any command:
    vendor/bin/canvas make:model --help
    

Extension Points

  1. Custom Commands: Extend Canvas by creating new stubs and commands. Example: Add a make:resource-collection command by:

    • Creating resources/stubs/resource-collection.stub.
    • Publishing a custom command class (see Canvas Core).
  2. Preset Templates: Share custom presets across projects by extending the preset command. Example:

    vendor/bin/canvas preset custom --namespace="App" --paths.src="src"
    
  3. Integration with IDEs: Use Canvas in IDE templates (e.g., PHPStorm Live Templates) to auto-generate code snippets. Example:

    canvas make:model ${NAME} -m
    
  4. Git Hooks: Automate boilerplate generation in pre-commit hooks. Example .git/hooks/pre-commit:

    #!/bin/sh
    composer exec canvas make:factory "$(git diff --name-only | grep '\.php$' | head -1 | sed 's/\.php$//')"
    

Pro Tips

  • Monorepo Namespaces: Use --namespace to generate classes in shared namespaces:
    vendor/bin/canvas make:service PaymentProcessor --namespace="Company\Shared\Services"
    
  • Batch Generation: Chain commands for rapid scaffolding:
    vendor/bin/canvas make:model Post -m && \
    vendor/bin/canvas make:factory Post && \
    vendor/bin/canvas make:seeder PostSeeder
    
  • Laravel Packages: Combine with orchestra/testbench for testing:
    composer require --dev "orchestra/testbench"
    vendor/bin/canvas make:test PostTest --namespace="Tests\Unit"
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport