Overview
The Gallery feature provides a powerful photo management system with album organization. Built on Spatie Media Library, it supports automatic image conversions, thumbnails, and responsive image handling.Database Structure
- Albums Table
- Photos Table
- Album-Photo Pivot
The Auto-generated Slug: Automatically created from
album table organizes photos into collections:album_name using Str::slug()Reference: database/migrations/2025_03_31_154244_create_album_table.php:14Model Relationships
Album Model
TheAlbum model implements Spatie’s HasMedia interface:
app/Models/Album.php:12
Photo Model
ThePhoto model also integrates with Spatie Media Library:
app/Models/Photo.php:11
Spatie Media Library Integration
Media Storage
Images are stored using Spatie’s
media table:- Original files stored in filesystem
- Metadata tracked in database
- Automatic conversions generated
- Responsive image support
Image Conversions
Automatic preview generation:
- Preview Size: 300x300 pixels
- Fit Mode: Contain (maintains aspect ratio)
- Processing: Non-queued (immediate)
- Use Case: Gallery thumbnails
Admin Workflows
Managing Albums
Managing Albums
Album Management
-
View Albums
- Navigate to
/admin/gallery/albums - See all albums with photo count
- Albums loaded with
photosrelationship and media
- Navigate to
-
Create Album
- Resource route:
POST /admin/gallery/albums - Provide album name (slug auto-generated)
- Add optional description
- Resource route:
-
Album Features
- Slug-based URLs: SEO-friendly album routes like
/gallery/album/{slug} - Photo Count: Track number of photos per album
- Cover Photo: Set featured image for album preview
- Slug-based URLs: SEO-friendly album routes like
Admin\AlbumController@indexReference:
app/Http/Controllers/Admin/AlbumController.php:14Managing Photos
Managing Photos
Photo Management
-
View All Photos
- Route:
GET /admin/gallery/photos - Displays all photos with media relationships
- Shows preview thumbnails
- Route:
-
Upload New Photo
- Route:
POST /admin/gallery/photos - Submit title and description
- Upload image file (processed by Spatie)
- Route:
-
Edit Photo Details
- Route:
PATCH /admin/gallery/photos/{photo} - Update title and description
- Change album associations
- Route:
-
Delete Photo
- Route:
DELETE /admin/gallery/photos/{photo} - Removes photo record and associated media files
- Automatically cleans up album relationships
- Route:
Admin\PhotoControllerReference:
app/Http/Controllers/Admin/PhotoController.php:15Assigning Photos to Albums
Assigning Photos to Albums
Photo-Album Assignment
Photos can belong to multiple albums through the pivot table:database/migrations/2025_05_30_070106_create_album-photo_table.php:20Frontend Display
- Gallery Index
- Album Detail
- Single Image
Route:
Reference:
GET /galleryDisplays all albums to visitors:- Album grid layout
- Cover image for each album
- Album name and description
- Photo count badge
- Links to album detail pages
GalleryController@indexReference:
routes/web.php:62Media Conversions
Spatie Media Library automatically generates image versions:Preview Conversion
app/Models/Photo.php:27
Key Routes
Admin Routes
| Method | URI | Action | Description |
|---|---|---|---|
| GET | /admin/gallery/albums | AlbumController@index | List all albums |
| POST | /admin/gallery/albums | AlbumController@store | Create album |
| GET | /admin/gallery/albums/{album} | AlbumController@show | Show album |
| PATCH | /admin/gallery/albums/{album} | AlbumController@update | Update album |
| DELETE | /admin/gallery/albums/{album} | AlbumController@destroy | Delete album |
| GET | /admin/gallery/photos | PhotoController@index | List all photos |
| POST | /admin/gallery/photos | PhotoController@store | Upload photo |
| PATCH | /admin/gallery/photos/{photo} | PhotoController@update | Update photo |
| DELETE | /admin/gallery/photos/{photo} | PhotoController@destroy | Delete photo |
Frontend Routes
| Method | URI | Action | Description |
|---|---|---|---|
| GET | /gallery | GalleryController@index | Gallery home |
| GET | /gallery/album/{album:slug} | GalleryController@album | Album photos |
| GET | /gallery/album/{album:slug}/{image} | GalleryController@image | Single image |
| GET | /image/{photo} | PhotoController@show | Direct photo view |
routes/web.php:61-68
Performance Considerations
Eager Loading
The album index uses eager loading to prevent N+1 queries:Reference:
app/Http/Controllers/Admin/AlbumController.php:16Media Collections
Photos use dedicated media collection:
- Collection name:
images - Prevents media type mixing
- Enables collection-specific processing
app/Models/Photo.php:23