# Blog Module Quick Reference

## Database Schema

**blogs** table
- id, uid (unique), title, slug (unique), description, theme_category_id, media_id, type, status, timestamps, soft_deletes

**blog_details** table  
- id, blog_id (FK→blogs), language_id (FK→languages), title, slug (unique), description, media_id (FK→media), view_count, timestamps, soft_deletes

## Models

**Blog**
```php
Blog::with('detail', 'media', 'themeCategory')->find($id)
$blog->detail // BlogDetail (1:1)
```

**BlogDetail**
```php
BlogDetail::where('blog_id', $blogId)->first()
$detail->incrementViewCount() // Increment views
$detail->blog // Blog (belongs to)
$detail->language // Language (belongs to)
```

## Filtering

```php
// Blog search
Blog::filter(['search' => 'Laravel'])->paginate()

// BlogDetail search
BlogDetail::filter([
    'search' => 'query',
    'language' => 1,
    'blog' => 123,
    'viewCountMin' => 10,
    'viewCountMax' => 100
])->paginate()
```

## API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | /v1/blogs | List blogs |
| POST | /v1/blogs | Create blog |
| GET | /v1/blogs/{id} | Show blog |
| PUT | /v1/blogs/{id} | Update blog |
| DELETE | /v1/blogs/{id} | Delete blog |
| GET | /v1/blogs/{blogId}/detail | Get detail (increments views) |
| PUT | /v1/blogs/{blogId}/detail | Update detail |
| GET | /v1/blog-details | List details |

## Quick Examples

### Create Blog with Detail
```php
$blog = Blog::create(['title' => 'Post', 'slug' => 'post', ...]);
BlogDetail::create(['blog_id' => $blog->id, 'language_id' => 1, ...]);
```

### Get with Details
```php
$blog = Blog::with('detail')->find(1);
echo $blog->detail->description;
echo $blog->detail->view_count;
```

### Filter Popular Posts
```php
$popular = BlogDetail::filter(['viewCountMin' => 50])->orderBy('view_count', 'desc')->paginate();
```

## Files Structure

```
Website/Blog/
├── app/Models/
│   ├── Blog.php
│   └── BlogDetail.php
├── app/Http/Controllers/
│   ├── BlogController.php
│   └── BlogDetailController.php
├── app/Transformers/
│   ├── BlogResource.php
│   ├── BlogDetailResource.php
│   └── BlogBlogResource.php
├── app/ModelFilters/
│   ├── BlogFilter.php
│   └── BlogDetailFilter.php
├── database/
│   ├── migrations/
│   │   ├── 2025_12_07_105710_create_blogs_table.php
│   │   └── 2026_04_05_000001_create_blog_details_table.php
│   ├── factories/BlogDetailFactory.php
│   └── seeders/BlogDatabaseSeeder.php
├── routes/api.php
└── BLOG_IMPLEMENTATION.md
```

## Key Methods

```php
// BlogDetail
$detail->incrementViewCount()         // +1 view
$detail->blog()                       // Get blog
$detail->language()                   // Get language
$detail->media()                      // Get media

// Blog
$blog->detail()                       // Get blog detail
$blog->media()                        // Get media
$blog->themeCategory()                // Get theme
```

## Seeders & Factories

```bash
# Seed data
php artisan db:seed --class="Website\Blog\Database\Seeders\BlogDatabaseSeeder"

# Factory usage
$detail = BlogDetail::factory()->create(['blog_id' => 1])
```

## Notes

- BlogDetail increments view_count automatically on retrieval via API
- Both tables support soft deletes
- Activity logging enabled on both models
- Filterable trait on both models for advanced queries
- Multi-language support via blog_details.language_id
- Cascade delete: Blog deletion removes BlogDetail
- Null on delete: Language/Media deletion clears FK
