feat: add tour enums, update Tour model, and create TourImage relation

This commit is contained in:
2026-08-20 11:58:45 +03:30
parent 3867350331
commit 021a66cfc9
5 changed files with 133 additions and 9 deletions
+24 -9
View File
@@ -6,11 +6,15 @@
namespace App\Models;
use App\Enums\Admin\Tour\TourMode;
use App\Enums\Admin\Tour\TourScope;
use App\Enums\Admin\Tour\TourType;
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
@@ -34,6 +38,7 @@ use Illuminate\Database\Eloquent\Model;
*/
class Tour extends Model
{
use SoftDeletes;
protected $table = 'tours';
public static $snakeAttributes = false;
@@ -41,8 +46,9 @@ class Tour extends Model
'origin_city_id' => 'int',
'destination_city_id' => 'int',
'file_id' => 'int',
'tour_mode'=> 'int',
'tour_scope' => 'int',
'tour_mode'=> TourMode::class,
'tour_scope' => TourScope::class,
'tour_type' => TourType::class,
'views_count' => 'int'
];
@@ -61,18 +67,16 @@ class Tour extends Model
#[Scope]
protected function applySort(Builder $query, ?string $sort = null): void
{
$query->withMin('tourDepartures', 'price')
->withMax('tourDepartures', 'price');
switch ($sort) {
case 'lowest':
$query->withMin('tourDepartures', 'price')
->orderBy('tour_departures_min_price');
$query->orderBy('tour_departures_min_price');
break;
case 'highest':
$query->withMax('tourDepartures', 'price')
->orderByDesc('tour_departures_max_price');
$query->orderByDesc('tour_departures_max_price');
break;
default:
$query->orderByDesc('id');
break;
@@ -106,4 +110,15 @@ class Tour extends Model
{
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);
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class TourImage
*
* @property int $id
* @property int $tour_id
* @property int $file_id
* @property int|null $sort_order
* @property bool|null $is_default
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @package App\Models
*/
class TourImage extends Model
{
protected $table = 'tour_images';
public static $snakeAttributes = false;
protected $casts = [
'tour_id' => 'int',
'file_id' => 'int',
'sort_order' => 'int',
'is_default' => 'bool'
];
protected $fillable = [
'tour_id',
'file_id',
'sort_order',
'is_default'
];
public function tour()
{
return $this->belongsTo(Tour::class);
}
public function file()
{
return $this->belongsTo(File::class);
}
}