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

coral/content-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle to your Laravel project via Composer:

    composer require atlantic18/coral-content-bundle
    

    Register the bundle in config/app.php under providers:

    Atlantic18\CoralContentBundle\CoralContentBundle::class,
    
  2. Publish Config & Migrations Run:

    php artisan vendor:publish --provider="Atlantic18\CoralContentBundle\CoralContentBundle" --tag="config"
    php artisan vendor:publish --provider="Atlantic18\CoralContentBundle\CoralContentBundle" --tag="migrations"
    

    Migrate the database:

    php artisan migrate
    
  3. First Use Case: Creating a Content Type Define a content type in config/coral_content.php:

    'content_types' => [
        'article' => [
            'fields' => [
                'title' => ['type' => 'text'],
                'body' => ['type' => 'textarea'],
                'published_at' => ['type' => 'datetime'],
            ],
        ],
    ],
    

    Create a content item via Trait:

    use Atlantic18\CoralContentBundle\Traits\HasCoralContent;
    
    class Article extends Model
    {
        use HasCoralContent;
    }
    
    $article = Article::createContent('article', [
        'title' => 'Hello World',
        'body' => 'Content here...',
    ]);
    

Implementation Patterns

Workflows

  1. Content Management

    • CRUD via Eloquent: Use the HasCoralContent trait to interact with content types as models.
      // Fetch all articles
      $articles = Article::all();
      
      // Update content
      $article->updateContent(['title' => 'Updated Title']);
      
    • Versioning: Leverage the versions() method to track changes:
      $versions = $article->versions();
      
  2. API Integration

    • RESTful Endpoints: Use Laravel's API resources to expose content:
      // routes/api.php
      Route::apiResource('articles', ArticleController::class);
      
    • GraphQL: Integrate with Laravel GraphQL (e.g., nWidart/laravel-modules) for flexible queries:
      query {
        articles {
          title
          body
        }
      }
      
  3. Frontend Integration

    • Blade Templates: Fetch and render content dynamically:
      @foreach($articles as $article)
          <h2>{{ $article->title }}</h2>
          <div>{{ $article->body }}</div>
      @endforeach
      
    • Livewire/Alpine: Use for real-time content updates or interactive forms.
  4. Scheduled Publishing

    • Use Laravel's scheduler to auto-publish content:
      // app/Console/Kernel.php
      $schedule->call(function () {
          \Atlantic18\CoralContentBundle\Models\Content::publishOverdue();
      })->hourly();
      

Integration Tips

  • Laravel Scout: Index content for fast search:
    use Laravel\Scout\Searchable;
    
    class Article extends Model
    {
        use HasCoralContent, Searchable;
    }
    
  • Media Library: Integrate with spatie/laravel-medialibrary for rich media fields:
    'fields' => [
        'image' => ['type' => 'media'],
    ],
    
  • Localization: Extend the bundle to support multilingual content via laravel-localization.

Gotchas and Tips

Pitfalls

  1. Migration Conflicts

    • If you modify config/coral_content.php after migrations run, manually adjust the contents table schema or drop/recreate it:
      php artisan migrate:fresh
      
    • Tip: Use --seed to repopulate test data if needed.
  2. Content Type Validation

    • The bundle lacks built-in validation for custom fields. Extend the Content model or use Laravel's FormRequest:
      use Illuminate\Validation\Rule;
      
      public function rules()
      {
          return [
              'title' => ['required', Rule::unique('contents')->where('type', 'article')],
          ];
      }
      
  3. Performance with Large Datasets

    • Avoid eager-loading all content versions for performance:
      // Bad: Loads all versions for every article
      $articles = Article::with('versions')->get();
      
      // Good: Load only necessary versions
      $article = Article::with(['versions' => function($query) {
          $query->where('id', $latestVersionId);
      }])->find($id);
      
  4. Caching Content

    • Cache content responses in API routes or Blade views:
      Route::get('/articles', function () {
          return Cache::remember('articles', now()->addHours(1), function () {
              return Article::all();
          });
      });
      

Debugging

  • Log Content Changes: Override the Content model to log changes:
    protected static function boot()
    {
        parent::boot();
        static::updating(function ($model) {
            \Log::info('Content updated', ['type' => $model->type, 'id' => $model->id]);
        });
    }
    
  • Check Database Triggers: Ensure no triggers conflict with the bundle's migrations.

Extension Points

  1. Custom Field Types Extend the bundle by adding new field types in app/Providers/CoralContentServiceProvider.php:

    public function register()
    {
        $this->app->extend('coral_content.fields', function ($fields) {
            $fields['custom'] = \App\Fields\CustomField::class;
            return $fields;
        });
    }
    
  2. Content Events Listen for content events (e.g., content.created):

    // app/Providers/EventServiceProvider.php
    protected $listen = [
        \Atlantic18\CoralContentBundle\Events\ContentCreated::class => [
            \App\Listeners\LogContentCreation::class,
        ],
    ];
    
  3. Admin Panel Integration Use backpack/laravel-backpack or filamentphp/filament to build a custom admin interface for managing content types.

  4. Testing Use Laravel's testing helpers to assert content:

    $this->assertDatabaseHas('contents', [
        'type' => 'article',
        'data->title' => 'Test Article',
    ]);
    
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