Files
thrillwiki_laravel/memory-bank/models/ParkModelEnhancements.md

4.8 KiB

Park Model Enhancements

Implemented Features

1. Photos Relationship

  • Created a Photo model with polymorphic relationships
  • Added a morphMany relationship to the Park model
  • Implemented methods for adding/removing photos
  • Added support for featured photos

2. Get By Slug Method

  • Implemented the getBySlug static method to find parks by current or historical slugs
  • Returns both the park and a boolean indicating if a historical slug was used

3. Absolute URL Method

  • Implemented the getAbsoluteUrl method to generate the URL for the park detail page
  • Uses Laravel's route() helper with the park's slug

4. Formatted Location Property

  • Added a getFormattedLocationAttribute method to the Park model
  • Uses the existing Location relationship
  • Returns a formatted address string

5. Coordinates Property

  • The getCoordinatesAttribute method was already implemented in the HasLocation trait
  • Returns an array with latitude and longitude

6. Media Management

  • Implemented methods for adding photos to parks
  • Added photo ordering functionality
  • Added methods for setting a featured photo
  • Implemented featured photo display for park cards

Implementation Details

Photo Model

The Photo model implements a polymorphic relationship that can be used with any model that needs photos:

class Photo extends Model
{
    // Attributes and casts...
    
    public function photoable(): MorphTo
    {
        return $this->morphTo();
    }
    
    // Methods for managing photos...
}

Park Model Photo Relationship

The Park model now has a morphMany relationship to photos:

public function photos(): MorphMany
{
    return $this->morphMany(Photo::class, 'photoable');
}

Photo Management Methods

Added methods to the Park model for managing photos:

  • addPhoto(array $attributes): Add a new photo to the park
  • setFeaturedPhoto($photo): Set a photo as the featured photo
  • reorderPhotos(array $photoIds): Reorder photos by position
  • featuredPhoto(): Get the featured photo for the park
  • getFeaturedPhotoUrlAttribute(): Get the URL of the featured photo or a default image

Slug History Support

Enhanced the Park model with a method to find parks by current or historical slugs:

public static function getBySlug(string $slug): array
{
    // Try current slug
    $park = static::where('slug', $slug)->first();
    if ($park) {
        return [$park, false];
    }

    // Try historical slug
    $slugHistory = SlugHistory::where('slug', $slug)
        ->where('sluggable_type', static::class)
        ->latest()
        ->first();

    if ($slugHistory) {
        $park = static::find($slugHistory->sluggable_id);
        return [$park, true];
    }

    return [null, false];
}

Location Support

Added a formatted location property to the Park model:

public function getFormattedLocationAttribute(): string
{
    if ($this->location) {
        return $this->formatted_address ?? '';
    }
    
    return '';
}

Database Changes

Created a new migration for the photos table with polymorphic relationships:

Schema::create('photos', function (Blueprint $table) {
    $table->id();
    $table->morphs('photoable');
    $table->string('title')->nullable();
    $table->text('description')->nullable();
    $table->string('file_path');
    $table->string('file_name');
    $table->integer('file_size')->nullable();
    $table->string('mime_type')->nullable();
    $table->integer('width')->nullable();
    $table->integer('height')->nullable();
    $table->integer('position')->default(0);
    $table->boolean('is_featured')->default(false);
    $table->string('alt_text')->nullable();
    $table->string('credit')->nullable();
    $table->string('source_url')->nullable();
    $table->json('metadata')->nullable();
    $table->timestamps();
});

Implementation Challenges and Solutions

1. Photo Model and Relationships

  • Created a polymorphic relationship between Photo and other models
  • Implemented methods for managing photos (adding, setting featured, reordering)
  • Added proper position handling for photo ordering
  • Ensured is_featured flag is properly set when adding new photos

2. Slug History Handling

  • Fixed the HasSlugHistory trait to use model instances instead of static methods
  • Implemented proper historical slug tracking and lookup

3. Location Integration

  • Removed the unsupported point cast from the Location model
  • Fixed the formatted location property to properly use the location relationship

4. Testing

  • Created comprehensive tests for all new functionality
  • Fixed test cases to match the actual implementation
  • Ensured all tests pass with PostgreSQL database

Next Steps

  1. Create a controller for managing photos
  2. Implement file upload functionality
  3. Update the park detail page to display photos
  4. Create a photo gallery component
  5. Add photo management UI