Files

119 lines
2.8 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class Tour
*
* @property int $id
* @property int|null $travel_type_id
* @property string $title
* @property string|null $slug
* @property string|null $image
* @property string|null $description
* @property string|null $duration
* @property Carbon|null $start_date
* @property Carbon|null $end_date
* @property float $price
* @property int|null $capacity
* @property int $views_count
* @property int $status
* @property Carbon $created_at
* @property Carbon $updated_at
*
* @property TravelType|null $travelType
* @property Collection|Flight[] $flights
*
* @package App\Models
*/
class Tour extends Model
{
protected $table = 'tours';
public static $snakeAttributes = false;
protected $casts = [
'travel_type_id' => 'int',
'start_date' => 'datetime',
'end_date' => 'datetime',
'price' => 'float',
'capacity' => 'int',
'views_count' => 'int',
'status' => 'int'
];
protected $fillable = [
'travel_type_id',
'title',
'slug',
'image',
'description',
'duration',
'start_date',
'end_date',
'price',
'capacity',
'views_count',
'status'
];
public function travelType()
{
return $this->belongsTo(TravelType::class);
}
public function flights()
{
return $this->belongsToMany(Flight::class, 'tour_flights')
->withPivot('id')
->withTimestamps();
}
// در فایل App\Models\Tour.php و App\Models\Flight.php
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function favorites()
{
return $this->morphMany(\App\Models\Favorite::class, 'favoritable');
}
public function isFavoritedBy($user)
{
if (!$user) return false;
// استفاده از فیلدهای پلی‌مورفیک به جای tour_id
return $this->favorites()
->where('user_id', $user->id)
->where('favoritable_id', $this->id)
->where('favoritable_type', Tour::class)
->exists();
}
protected static function booted()
{
static::creating(function ($tour) {
// اگر مقداری تعیین نشده بود، پیش‌فرض‌ها را اعمال کن
if (is_null($tour->views_count)) {
$tour->views_count = 0;
}
if (is_null($tour->status)) {
// فرض بر اینکه 0 یعنی غیرفعال یا پیش‌نویس
$tour->status = 0;
}
});
}
}