126 lines
2.9 KiB
PHP
126 lines
2.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
use App\Enums\Flight\TourType;
|
|
use App\Enums\Tour\TourMode;
|
|
use App\Enums\Tour\TourScope;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* Class Tour
|
|
*
|
|
* @property int $id
|
|
* @property string $title
|
|
* @property string $summary
|
|
* @property string $description
|
|
* @property int $origin_city_id
|
|
* @property int $destination_city_id
|
|
* @property int $file_id
|
|
* @property string $tour_type
|
|
* @property Carbon $created_at
|
|
* @property Carbon $updated_at
|
|
*
|
|
* @property Flight $flight
|
|
* @property City $city
|
|
* @property Collection|TourDeparture[] $tourDepartures
|
|
*
|
|
* @package App\Models
|
|
*/
|
|
class Tour extends Model
|
|
{
|
|
use SoftDeletes;
|
|
protected $table = 'tours';
|
|
public static $snakeAttributes = false;
|
|
|
|
protected $casts = [
|
|
'origin_city_id' => 'int',
|
|
'destination_city_id' => 'int',
|
|
'file_id' => 'int',
|
|
'tour_mode'=> TourMode::class,
|
|
'tour_scope' => TourScope::class,
|
|
'tour_type' => TourType::class,
|
|
'views_count' => 'int'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'title',
|
|
'summary',
|
|
'description',
|
|
'origin_city_id',
|
|
'destination_city_id',
|
|
'file_id',
|
|
'tour_type',
|
|
'tour_mode',
|
|
'tour_scope',
|
|
'views_count'
|
|
];
|
|
#[Scope]
|
|
protected function applySort(Builder $query, ?string $sort = null): void
|
|
{
|
|
$query->withMin('tourDepartures', 'price')
|
|
->withMax('tourDepartures', 'price');
|
|
|
|
switch ($sort) {
|
|
case 'lowest':
|
|
$query->orderBy('tour_departures_min_price');
|
|
break;
|
|
case 'highest':
|
|
$query->orderByDesc('tour_departures_max_price');
|
|
break;
|
|
default:
|
|
$query->orderByDesc('id');
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function originCity()
|
|
{
|
|
return $this->belongsTo(City::class, 'origin_city_id');
|
|
}
|
|
|
|
public function city()
|
|
{
|
|
return $this->belongsTo(City::class, 'destination_city_id');
|
|
}
|
|
|
|
public function tourDepartures()
|
|
{
|
|
return $this->hasMany(TourDeparture::class , 'tour_id');
|
|
}
|
|
|
|
public function file()
|
|
{
|
|
return $this->belongsTo(File::class, 'file_id');
|
|
}
|
|
public function comments()
|
|
{
|
|
return $this->hasMany(Comment::class, 'tour_id');
|
|
}
|
|
public function favorites()
|
|
{
|
|
return $this->hasMany(Favorite::class);
|
|
}
|
|
public function images()
|
|
{
|
|
return $this->hasMany(TourImage::class)
|
|
->orderBy('sort_order');
|
|
}
|
|
|
|
public function coverImage()
|
|
{
|
|
return $this->hasOne(TourImage::class)
|
|
->where('is_default', true);
|
|
}
|
|
}
|