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

Product Bundle Laravel Package

ekyna/product-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require ekyna/product-bundle
    

    Add to config/app.php under providers:

    Ekyna\ProductBundle\ProductBundleServiceProvider::class,
    

    Publish the config (if available):

    php artisan vendor:publish --provider="Ekyna\ProductBundle\ProductBundleServiceProvider"
    
  2. First Use Case

    • Run migrations (if included):
      php artisan migrate
      
    • Create a product via Tinker or a controller:
      use Ekyna\ProductBundle\Models\Product;
      
      $product = Product::create([
          'name' => 'Test Product',
          'sku' => 'SKU123',
          'price' => 19.99,
      ]);
      
  3. Where to Look First

    • Models: Check app/Models/Product.php (or equivalent) for core logic.
    • Migrations: Review database/migrations/ for schema details.
    • Service Layer: Look for ProductService or similar in app/Services/.

Implementation Patterns

Core Workflows

  1. CRUD Operations Use Eloquent methods directly or leverage a service layer:

    // Controller example
    public function store(Request $request) {
        $validated = $request->validate([
            'name' => 'required',
            'sku' => 'required|unique:products',
            'price' => 'numeric',
        ]);
        return Product::create($validated);
    }
    
  2. Product Attributes Extend the Product model to handle custom attributes:

    // Add to Product model
    protected $appends = ['formatted_price'];
    public function getFormattedPriceAttribute() {
        return '$' . number_format($this->price, 2);
    }
    
  3. Inventory Management Integrate with inventory systems via events:

    // In Product model
    protected static function booted() {
        static::created(function ($product) {
            event(new ProductCreated($product));
        });
    }
    
  4. API Endpoints Use Laravel’s API resources:

    // ProductResource.php
    public function toArray($request) {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'price' => $this->price,
            'sku' => $this->sku,
        ];
    }
    

Integration Tips

  • Validation: Reuse Laravel’s validation rules or extend the Product model with custom rules.
  • Relationships: Define relationships (e.g., belongsToMany for categories) in the Product model.
  • Observers: Use Eloquent observers for pre/post actions:
    // app/Observers/ProductObserver.php
    public function saving(Product $product) {
        $product->slug = Str::slug($product->name);
    }
    

Gotchas and Tips

Pitfalls

  1. Missing Documentation

    • Issue: No clear setup instructions or API docs.
    • Fix: Inspect the Product model and migrations for clues. Check for undocumented config keys.
  2. No Default Config

    • Issue: The TODO in the README suggests missing config options.
    • Fix: Override defaults in config/product.php (if published) or hardcode in the service provider.
  3. Lack of Testing

    • Issue: No tests or examples may lead to edge-case bugs.
    • Fix: Write unit tests for critical paths (e.g., price validation, SKU uniqueness).
  4. No Events/Listeners

    • Issue: Missing hooks for inventory or notification systems.
    • Fix: Extend the Product model with custom events or use Laravel’s dispatch method.

Debugging Tips

  • Check Migrations: Ensure tables match your expectations (e.g., products table exists).
  • Log Model Events: Add debug logs in observers or model hooks:
    public function saved(Product $product) {
        \Log::debug("Product saved: ", $product->toArray());
    }
    
  • Validate Dependencies: Confirm no hidden dependencies (e.g., ekyna/* packages).

Extension Points

  1. Custom Fields Use Laravel’s morphTo/morphWith for dynamic attributes or add a product_attributes table.

  2. API Versioning Override routes in routes/api.php to version endpoints:

    Route::prefix('v1')->group(function () {
        Route::apiResource('products', ProductController::class);
    });
    
  3. Localization Add locale and translated_name fields, then use Laravel’s localization helpers.

  4. Caching Cache product lists or details:

    $products = Cache::remember('products.list', now()->addHours(1), function () {
        return Product::all();
    });
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware