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

Content Bundle Laravel Package

bitcreator/content-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require bitcreator/content-bundle
    

    Publish the configuration (if needed):

    php artisan vendor:publish --provider="BitCreator\ContentBundle\ContentBundleServiceProvider"
    
  2. First Use Case: Basic Content Model Define a content model in a migration:

    Schema::create('content_items', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('body')->nullable();
        $table->string('slug')->unique();
        $table->timestamps();
    });
    

    Create a model extending BitCreator\ContentBundle\Models\ContentModel:

    namespace App\Models;
    
    use BitCreator\ContentBundle\Models\ContentModel;
    
    class ContentItem extends ContentModel
    {
        protected $table = 'content_items';
    }
    
  3. Basic CRUD Operations

    // Create
    $item = new \App\Models\ContentItem();
    $item->title = 'Hello World';
    $item->body = 'This is a test.';
    $item->save();
    
    // Retrieve
    $item = \App\Models\ContentItem::find(1);
    
    // Update
    $item->title = 'Updated Title';
    $item->save();
    
    // Delete
    $item->delete();
    
  4. Leveraging Bundle Features Check the config/content-bundle.php for default settings (e.g., caching, slug generation rules).


Implementation Patterns

1. Content Management Workflows

  • Slug Generation Use the built-in sluggable trait for automatic slug creation:

    use BitCreator\ContentBundle\Traits\Sluggable;
    
    class ContentItem extends ContentModel
    {
        use Sluggable;
    
        protected $slugField = 'slug';
        protected $slugSource = 'title';
    }
    
  • Versioning Enable versioning in the model:

    class ContentItem extends ContentModel
    {
        protected $versioned = true;
    }
    

    Access versions via:

    $versions = $item->versions()->get();
    
  • Localization If the bundle supports localization, define translatable fields:

    class ContentItem extends ContentModel
    {
        protected $translatable = ['title', 'body'];
    }
    

2. Integration with Laravel Ecosystem

  • API Resources Create a resource for API responses:

    namespace App\Http\Resources;
    
    use App\Models\ContentItem;
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class ContentItemResource extends JsonResource
    {
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                'title' => $this->title,
                'slug' => $this->slug,
                'body' => $this->body,
                'created_at' => $this->created_at,
            ];
        }
    }
    
  • Form Requests Validate content submissions:

    namespace App\Http\Requests;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class StoreContentItemRequest extends FormRequest
    {
        public function rules()
        {
            return [
                'title' => 'required|string|max:255',
                'body' => 'nullable|string',
            ];
        }
    }
    
  • Events & Observers Listen to content events (e.g., ContentCreated, ContentUpdated):

    namespace App\Listeners;
    
    use BitCreator\ContentBundle\Events\ContentCreated;
    
    class LogContentCreation
    {
        public function handle(ContentCreated $event)
        {
            \Log::info('New content created: ' . $event->content->title);
        }
    }
    

3. Customizing Behavior

  • Extend ContentModel Override methods for custom logic:

    class ContentItem extends ContentModel
    {
        public function getFullTitleAttribute()
        {
            return "Published: " . $this->title;
        }
    }
    
  • Query Scopes Add reusable query logic:

    class ContentItem extends ContentModel
    {
        public function scopePublished($query)
        {
            return $query->where('published_at', '<=', now());
        }
    }
    
  • Middleware for Content Access Protect routes with middleware:

    Route::get('/content/{item}', function (ContentItem $item) {
        return $item;
    })->middleware('can:view,content');
    

Gotchas and Tips

Pitfalls

  1. Slug Conflicts

    • If slugs are auto-generated, ensure slugSource is unique per model.
    • Handle duplicates manually if needed:
      $item->slug = \Str::slug($item->title) . '-' . rand(100, 999);
      
  2. Versioning Overhead

    • Versioning adds database load. Disable for non-critical models:
      protected $versioned = false;
      
  3. Caching Quirks

    • Clear cache after bulk updates:
      php artisan cache:clear
      
    • Configure cache drivers in config/content-bundle.php.
  4. Localization Pitfalls

    • Ensure database supports UTF-8 for multilingual content.
    • Test translations in all supported locales early.

Debugging Tips

  • Enable Query Logging Add to AppServiceProvider:

    \DB::enableQueryLog();
    $queries = \DB::getQueryLog();
    
  • Check for Silent Failures Wrap bundle operations in try-catch:

    try {
        $item->save();
    } catch (\Exception $e) {
        \Log::error('Content save failed: ' . $e->getMessage());
    }
    
  • Verify Model Bindings Ensure route model binding works:

    Route::get('/content/{item}', function (ContentItem $item) {
        // $item will be null if binding fails
    });
    

Extension Points

  1. Custom Storage Engines Override storage logic in ContentModel:

    protected function getStoragePath()
    {
        return storage_path('app/content/' . $this->slug);
    }
    
  2. Add Fields Dynamically Use model events to attach metadata:

    class ContentItem extends ContentModel
    {
        protected static function booted()
        {
            static::created(function ($item) {
                $item->metadata()->create(['key' => 'author', 'value' => auth()->id()]);
            });
        }
    }
    
  3. Hook into Bundle Events Listen for core events (e.g., ContentSaving, ContentDeleting):

    \Event::listen('BitCreator\ContentBundle\Events\ContentSaving', function ($event) {
        $event->content->touch();
    });
    
  4. Override Default Config Publish and extend the config:

    'slug' => [
        'max_length' => 150, // Override default
    ],
    
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.
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope