mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 02:51:09 -05:00
- Added rides index view with search and filter options. - Created rides show view to display ride details. - Implemented API routes for rides. - Developed authentication routes for user registration, login, and email verification. - Created tests for authentication, email verification, password reset, and user profile management. - Added feature tests for rides and operators, including creation, updating, deletion, and searching. - Implemented soft deletes and caching for rides and operators. - Enhanced manufacturer and operator model tests for various functionalities.
129 lines
3.0 KiB
PHP
129 lines
3.0 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;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Manufacturer extends Model
|
|
{
|
|
use HasFactory, HasSlugHistory, SoftDeletes;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'website',
|
|
'headquarters',
|
|
'description',
|
|
'total_rides',
|
|
'total_roller_coasters',
|
|
'is_active',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'total_rides' => 'integer',
|
|
'total_roller_coasters' => 'integer',
|
|
'is_active' => 'boolean',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Get the rides manufactured by this company.
|
|
*/
|
|
public function rides(): HasMany
|
|
{
|
|
return $this->hasMany(Ride::class, 'manufacturer_id');
|
|
}
|
|
|
|
/**
|
|
* Update ride statistics.
|
|
*/
|
|
public function updateStatistics(): void
|
|
{
|
|
$this->total_rides = $this->rides()->count();
|
|
$this->total_roller_coasters = $this->rides()
|
|
->where('category', 'RC')
|
|
->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);
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include active manufacturers.
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
/**
|
|
* Scope a query for optimized loading with statistics.
|
|
*/
|
|
public function scopeOptimized($query)
|
|
{
|
|
return $query->select(['id', 'name', 'slug', 'total_rides', 'total_roller_coasters', 'is_active']);
|
|
}
|
|
|
|
/**
|
|
* Get the route key for the model.
|
|
*/
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'slug';
|
|
}
|
|
} |