Files

154 lines
3.3 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* Class FlightController
*
* @property int $id
* @property string|null $airline
* @property string|null $flight_number
* @property string|null $origin
* @property string|null $destination
* @property Carbon|null $departure_date
* @property Carbon|null $departure_time
* @property Carbon|null $arrival_time
* @property string|null $flight_class
* @property string|null $image
* @property string $views
* @property int|null $capacity
* @property float|null $price
* @property int $type
* @property int|null $status
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @package App\Models
*/
class Flight extends Model
{
protected $table = 'flights';
public static $snakeAttributes = false;
public $timestamps = true;
protected $casts = [
'departure_date' => 'datetime',
'departure_time' => 'datetime',
'arrival_time' => 'datetime',
'capacity' => 'int',
'price' => 'float',
'type' => 'int',
'status' => 'int',
];
protected $attributes = [
'views' => 0,
];
protected $fillable = [
'airline',
'flight_number',
'origin',
'destination',
'departure_date',
'departure_time',
'arrival_time',
'flight_class',
'capacity',
'price',
'image',
'views',
'type',
'status'
];
public function travelType()
{
return $this->belongsTo(TravelType::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function favorites()
{
return $this->morphMany(Favorite::class, 'favoritable');
}
public function reservations()
{
return $this->morphMany(
Reservation::class, 'reservable');
}
#[Scope]
public function applySearch(Builder $query, ?array $filters = []): void
{
$query->when($filters['search'] ?? null, function ($query, $search) {
$query->where(function ($q) use ($search) {
$q->where('airline', 'like', "%{$search}%")
->orWhere('flight_number', 'like', "%{$search}%")
->orWhere('origin', 'like', "%{$search}%")
->orWhere('destination', 'like', "%{$search}%");
});
});
$query->when($filters['origin'] ?? null, function ($query, $origin) {
$query->where('origin', $origin);
});
$query->when($filters['destination'] ?? null, function ($query, $destination) {
$query->where('destination', $destination);
});
}
#[Scope]
public function filterType(Builder $query, $type = null): void
{
$query->when($type !== null && $type !== '', function ($query) use ($type) {
$query->where('type', $type);
});
}
#[Scope]
public function sort(Builder $query, $sort = null): void
{
if ($sort == 'price_asc') {
$query->orderBy('price', 'asc');
}
if ($sort == 'price_desc') {
$query->orderBy('price', 'desc');
}
}
}