83 lines
2.5 KiB
PHP
83 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($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'));
|
|
}
|
|
|
|
}
|