git commit "implement flight management with search filter and sorting"

This commit is contained in:
2026-08-19 09:07:34 +03:30
parent 40dbf3b575
commit 700bd38c20
11 changed files with 903 additions and 237 deletions
+46 -9
View File
@@ -6,8 +6,6 @@
namespace App\Models;
use App\Enums\FlightStatus;
use App\Enums\FlightType;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
@@ -53,6 +51,10 @@ class Flight extends Model
'status' => 'int',
];
protected $attributes = [
'views' => 0,
];
protected $fillable = [
'airline',
'flight_number',
@@ -79,10 +81,12 @@ class Flight extends Model
{
return $this->hasMany(Comment::class);
}
public function favorites()
{
return $this->morphMany(Favorite::class, 'favoritable');
}
public function reservations()
{
return $this->morphMany(
@@ -90,15 +94,48 @@ class Flight extends Model
}
#[Scope]
public function applySearch(Builder $query, array $filters): void
public function applySearch(Builder $query, ?array $filters = []): void
{
$query
->when($filters['origin'] ?? null, function($query,$origin){
$query->where('origin',$origin);
})
$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}%");
->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');
}
}
}