Files
thrillwiki_laravel/app/Models/Manufacturer.php

98 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Traits\HasSlugHistory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Manufacturer extends Model
{
use HasFactory, HasSlugHistory;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'name',
'slug',
'website',
'headquarters',
'description',
'total_rides',
'total_roller_coasters',
];
/**
* Get the rides manufactured by this company.
* Note: This relationship will be properly set up when we implement the Rides system.
*/
public function rides(): HasMany
{
return $this->hasMany(Ride::class);
}
/**
* Update ride statistics.
*/
public function updateStatistics(): void
{
$this->total_rides = $this->rides()->count();
$this->total_roller_coasters = $this->rides()
->where('type', 'roller_coaster')
->count();
$this->save();
}
/**
* Get the manufacturer's name with total rides.
*/
public function getDisplayNameAttribute(): string
{
return "{$this->name} ({$this->total_rides} rides)";
}
/**
* Get formatted website URL (ensures proper URL format).
*/
public function getWebsiteUrlAttribute(): string
{
if (!$this->website) {
return '';
}
$website = $this->website;
if (!str_starts_with($website, 'http://') && !str_starts_with($website, 'https://')) {
$website = 'https://' . $website;
}
return $website;
}
/**
* Scope a query to only include major manufacturers (with multiple rides).
*/
public function scopeMajorManufacturers($query, int $minRides = 5)
{
return $query->where('total_rides', '>=', $minRides);
}
/**
* Scope a query to only include coaster manufacturers.
*/
public function scopeCoasterManufacturers($query)
{
return $query->where('total_roller_coasters', '>', 0);
}
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
}