diff --git a/app/Enums/FlightStatus.php b/app/Enums/FlightStatus.php new file mode 100644 index 0000000..6823785 --- /dev/null +++ b/app/Enums/FlightStatus.php @@ -0,0 +1,9 @@ +input('adult', 0) + + $request->input('children', 0); + + + $flights = Flight::query() + ->where('status', FlightStatus::ENABLE) + ->applyFilter() + ->when($request->filled('from-destination'), function (Builder $query) use ($request) { + $from = $request->input('from-destination'); + + $query->whereHas('departureAirport.city', function ($query) use ($from) { + $query->where('name', 'LIKE', "%$from%"); + }); + }) + ->when($request->filled('to-destination'), function (Builder $query) use ($request) { + $to = $request->input('to-destination'); + + $query->whereHas('arrivalAirport.city', function ($query) use ($to) { + $query->where('name', 'LIKE', "%$to%"); + }); + }) +// ->when($request->filled('journey-date'), function (Builder $query) use ($request) { +// $date = $request->input('journey-date'); +// +// $query->whereDate('departure_time', $date); +// }) + ->when($passengers > 0, function (Builder $query) use ($passengers) { + $query->where('capacity', '>=', $passengers); + }) + ->with([ + 'departureAirport.city', + 'arrivalAirport.city', + 'airline', + 'file', + ]) + ->orderByDesc('created_at') + ->paginate(); + + return view('flights.index', compact('flights')); + } + public function show(Flight $flight) + { + abort_if( + $flight->status !== FlightStatus::ENABLE->value, + 404 + ); + + $flight->increment('views'); + + $flight->load([ + 'departureAirport.city', + 'arrivalAirport.city', + 'airline', + 'file', + 'comments' => function ($query) { + $query + ->where('status', CommentStatus::APPROVED) + ->with('user') + ->orderByDesc('created_at'); + }, + ]); + + return view('flights.show', compact('flight')); + } } diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 045b840..f1fa087 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -7,6 +7,8 @@ namespace App\Models; use Carbon\Carbon; +use Illuminate\Contracts\Database\Query\Builder; +use Illuminate\Database\Eloquent\Attributes\Scope; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; @@ -51,6 +53,7 @@ class Flight extends Model 'departure_time' => 'datetime', 'price' => 'float', 'capacity' => 'int', + 'views' => 'int', 'status' => 'int' ]; @@ -61,6 +64,7 @@ class Flight extends Model 'file_id', 'title', 'summary', + 'views', 'flight_type', 'departure_time', 'price', @@ -102,4 +106,15 @@ class Flight extends Model { return $this->hasMany(Favorite::class); } + + + #[Scope] + protected function applyFilter(Builder $query): void + { + $request = request(); + + if ($request->filled('flight_type')) { + $query->where('flight_type', $request->input('flight_type')); + } + } } diff --git a/resources/views/components/flight.blade.php b/resources/views/components/flight.blade.php index 34bd12c..0d5f314 100644 --- a/resources/views/components/flight.blade.php +++ b/resources/views/components/flight.blade.php @@ -1,42 +1,65 @@ -
+
+
- + + + +
+
+
+ +

- {{ $flight->departure_time->toJalali()->format('Y/m/d H:i')}} + + {{ $flight->departure_time->toJalali()->format('Y/m/d H:i') }}

+
+
+
از + - {{ number_format($flight->price) }} تومان - + {{ number_format($flight->price) }} تومان +
+ +
+
+
-
- بیشتر پیدا کن -
-
+