85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\CommentStatus;
|
|
use App\Enums\FlightStatus;
|
|
use App\Models\Flight;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
|
|
class FlightController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$passengers =
|
|
$request->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($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,
|
|
404
|
|
);
|
|
|
|
$viewedFlights = session()->get('viewed_flights', []);
|
|
|
|
if (!in_array($flight->id, $viewedFlights)) {
|
|
|
|
$flight->increment('views');
|
|
|
|
$viewedFlights[] = $flight->id;
|
|
|
|
session()->put('viewed_flights', $viewedFlights);
|
|
}
|
|
|
|
$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'));
|
|
}
|
|
}
|