git commit "implement flight management with search filter and sorting"
This commit is contained in:
@@ -5,14 +5,20 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\FlightStoreRequest;
|
||||
use App\Models\Flight;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FlightController extends Controller
|
||||
{
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
$flights = Flight::latest()->paginate(4);
|
||||
$flights = Flight::query()
|
||||
->applySearch($request->all())
|
||||
->filterType($request->type)
|
||||
->sort($request->sort)
|
||||
->latest()
|
||||
->paginate(4);
|
||||
|
||||
return view('admin.flights.index', compact('flights'));
|
||||
}
|
||||
@@ -32,7 +38,6 @@ class FlightController extends Controller
|
||||
|
||||
$data = $request->validated();
|
||||
|
||||
|
||||
if ($request->hasFile('image')) {
|
||||
|
||||
$data['image'] = $request
|
||||
|
||||
@@ -23,65 +23,69 @@ class FlightStoreRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'airline'=>[
|
||||
'airline' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
'flight_number'=>[
|
||||
'required',
|
||||
'flight_number' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:50'
|
||||
],
|
||||
'origin'=>[
|
||||
'origin' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
'destination'=>[
|
||||
'destination' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
'departure_date'=>[
|
||||
'departure_date' => [
|
||||
'required',
|
||||
'date',
|
||||
'after_or_equal:today'
|
||||
],
|
||||
'departure_time'=>[
|
||||
'string',
|
||||
],
|
||||
'departure_time' => [
|
||||
'required',
|
||||
'date_format:H:i'
|
||||
],
|
||||
'arrival_time'=>[
|
||||
'arrival_time' => [
|
||||
'required'
|
||||
],
|
||||
'flight_class'=>[
|
||||
'flight_class' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:50'
|
||||
],
|
||||
'capacity'=>[
|
||||
'capacity' => [
|
||||
'required',
|
||||
'integer',
|
||||
'min:1'
|
||||
],
|
||||
'price'=>[
|
||||
'price' => [
|
||||
'required',
|
||||
'numeric',
|
||||
'min:0'
|
||||
],
|
||||
'image' => [
|
||||
'nullable',
|
||||
'image',
|
||||
'mimes:jpg,jpeg,png,webp',
|
||||
'max:2048'
|
||||
],
|
||||
'type'=>[
|
||||
'required',
|
||||
'integer',
|
||||
'in:0,2,3'
|
||||
],
|
||||
'status'=>[
|
||||
'image' => [
|
||||
'required',
|
||||
'image',
|
||||
'mimes:jpg,jpeg,png,webp',
|
||||
'max:2048'
|
||||
],
|
||||
'views'=>[
|
||||
'nullable',
|
||||
'integer',
|
||||
'min:0'
|
||||
],
|
||||
'type' => [
|
||||
'required',
|
||||
'integer',
|
||||
'in:0,2,3'
|
||||
],
|
||||
'status' => [
|
||||
'required',
|
||||
'in:0,1,2,3,4'
|
||||
]
|
||||
|
||||
+3
-1
@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class File
|
||||
*
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $extension
|
||||
@@ -39,4 +39,6 @@ class File extends Model
|
||||
'original_name',
|
||||
'path'
|
||||
];
|
||||
|
||||
|
||||
}
|
||||
|
||||
+46
-9
@@ -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');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,3 +9,17 @@ if (!function_exists('getUserFullName')) {
|
||||
return $user->first_name . ' ' . $user->last_name;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('getImageUrl')) {
|
||||
|
||||
function getImageUrl(?string $path): ?string
|
||||
{
|
||||
if (!$path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return \Illuminate\Support\Facades\Storage::url($path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user