mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 08:51:11 -05:00
Add models, enums, and services for user roles, theme preferences, slug history, and ID generation
This commit is contained in:
98
app/Models/Manufacturer.php
Normal file
98
app/Models/Manufacturer.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user