119 lines
2.4 KiB
PHP
119 lines
2.4 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\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* Class TourController
|
|
*
|
|
* @property int $id
|
|
* @property string $title
|
|
* @property string $summary
|
|
* @property string $description
|
|
* @property string $image
|
|
* @property string $views
|
|
* @property int $travel_type_id
|
|
* @property int $created_by
|
|
* @property float $price
|
|
* @property Carbon $departure_date
|
|
* @property int $status
|
|
* @property Carbon $created_at
|
|
* @property Carbon $updated_at
|
|
*
|
|
* @property Collection|Comment[] $comments
|
|
*
|
|
* @package App\Models
|
|
*/
|
|
class Tour extends Model
|
|
{
|
|
protected $table = 'tours';
|
|
public static $snakeAttributes = false;
|
|
|
|
protected $casts = [
|
|
'travel_type_id' => 'int',
|
|
'price' => 'float',
|
|
'departure_date' => 'datetime',
|
|
'status' => 'int'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'title',
|
|
'summary',
|
|
'description',
|
|
'image',
|
|
'views',
|
|
'travel_type_id',
|
|
'price',
|
|
'created_by',
|
|
'departure_date',
|
|
'status'
|
|
];
|
|
|
|
public function comments()
|
|
{
|
|
return $this->hasMany(Comment::class);
|
|
}
|
|
public function travelType()
|
|
{
|
|
return $this->belongsTo(TravelType::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('title', 'like', "%{$search}%")
|
|
->orWhere('summary', 'like', "%{$search}%");
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
|
|
#[Scope]
|
|
public function filterType(Builder $query, $type = null): void
|
|
{
|
|
$query->when($type !== null && $type !== '', function($query) use ($type){
|
|
|
|
$query->where('travel_type_id', $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');
|
|
|
|
}
|
|
}
|
|
}
|