mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 11:51:11 -05:00
4.3 KiB
4.3 KiB
HasLocation Trait
Overview
The HasLocation trait provides location management capabilities to Laravel models through a polymorphic relationship with the Location model. It enables models to have associated geographic data, including coordinates, address information, and location-based querying capabilities.
Features
Relationships
location()- MorphOne relationship to Location model- Automatic location deletion when parent model is deleted
Accessors
coordinates- Returns [lat, lng] arrayformatted_address- Returns formatted address stringmap_url- Returns Google Maps URL
Location Management
updateLocation(array $attributes)- Update or create locationsetCoordinates(float $lat, float $lng, ?float $elevation)- Set coordinatessetAddress(array $components)- Set address components
Distance & Search
distanceTo($model)- Calculate distance to another modelscopeNearby($query, $lat, $lng, $radius)- Find nearby modelsscopeInBounds($query, $ne, $sw)- Find models within bounds
Usage
Adding Location Support
use App\Traits\HasLocation;
class Park extends Model
{
use HasLocation;
}
Managing Locations with PostGIS
// Create/update location with PostGIS point
$park->updateLocation([
'address' => '123 Main St',
'city' => 'Orlando',
'state' => 'FL',
'country' => 'USA',
'coordinates' => DB::raw("ST_SetSRID(ST_MakePoint(-81.379234, 28.538336), 4326)")
]);
// Set coordinates directly (automatically creates PostGIS point)
$park->setCoordinates(28.538336, -81.379234);
// Access location data (automatically extracts from PostGIS point)
$coordinates = $park->coordinates; // Returns [lat, lng]
$address = $park->formatted_address;
$mapUrl = $park->map_url;
PostGIS Spatial Queries
// Find parks within 50km using ST_DWithin
$nearbyParks = Park::nearby(28.538336, -81.379234, 50)->get();
// Find parks in bounds using ST_MakeEnvelope
$parksInArea = Park::inBounds(
['lat' => 28.6, 'lng' => -81.2], // Northeast
['lat' => 28.4, 'lng' => -81.4] // Southwest
)->get();
// Calculate distance using ST_Distance
$distance = $parkA->distanceTo($parkB);
PostGIS Functions Used
ST_SetSRID- Set spatial reference system (4326 = WGS 84)ST_MakePoint- Create point geometry from coordinatesST_DWithin- Find points within distanceST_MakeEnvelope- Create bounding boxST_Within- Check if point is within boundsST_Distance- Calculate distance between points
Integration Points
Models Using Trait
- Park
- ParkArea
- (Future models requiring location)
Related Components
- Location model
- GeocodeService
- LocationSearchService
- LocationSelector component
- LocationDisplay component
Implementation Notes
Design Decisions
-
Automatic Cleanup
- Location records are automatically deleted when parent is deleted
- Prevents orphaned location records
- Maintains referential integrity
-
Flexible Updates
updateLocation()handles both create and update- Reduces code duplication
- Provides consistent interface
-
Coordinate Handling
- Consistent lat/lng format
- Optional elevation support
- Null handling for incomplete data
-
Query Scopes
- Chainable with other queries
- Consistent parameter formats
- Performance-optimized implementation
Performance Considerations
- Spatial indexing with PostGIS GiST index
- Native PostGIS distance calculations
- Geography vs Geometry type selection
- Geography for accurate distance calculations
- Geometry for faster bounding box queries
- Efficient spatial joins using PostGIS functions
- Eager loading recommended for lists
- Optimized polymorphic relationships
Security
- Input validation in setters
- Coordinate bounds checking
- Safe SQL distance calculations
Testing Strategy
Unit Tests
- Relationship management
- Coordinate handling
- Address formatting
- Distance calculations
Integration Tests
- Location updates
- Search queries
- Boundary queries
- Deletion cleanup
Future Enhancements
- Add elevation support
- Implement timezone handling
- Add boundary polygon support
- Create location history tracking
- Add batch location updates
- Implement geofencing
- Add location validation
- Create location sharing