From 4c60e5da69e9ba484e9c630ec634e72f695a8cbc Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" Date: Tue, 9 Nov 2021 17:51:02 +0300 Subject: [PATCH] Eager loading update for frontend controllers and widgets (#1348) * Update AirportController.php Eager load `files` * Update DashboardController.php Eager load `journal` for user , and `aircraft, arr_airport, comments, dpt_airport` for last_pirep. * Update FlightController.php Eager load + `alt_airport, subfleets` (with their airlines) for flights and `current_airport` for user (though current_airport can be removed and we can base the in blade checks on `$user->curr_airport_id` to reduce db reads and model loading) * Update HomeController.php Eager load `home_airport` for users * Update PirepController.php Eager load `airline, aircraft, fares, transactions, dpt_airport, arr_airport, comments, simbrief, user with rank` for pirep details * Update ProfileController.php Eager load `airline, awards, current_airport, fields.field, home_airport, last_pirep, rank` for user * Update UserController.php Eager load `airline, current_airport, fields.field, home_airport, rank` for users and count `awards` * Update AirportController.php * Update DashboardController.php * Update PirepController.php * Update ProfileController.php * Update LatestNews.php Eager load `user` for news * Update LatestPilots.php Eager load `home_airport` for latest users * StyleFix 1 * StlyeFix 1.5 * Update SimBriefController.php Eager load airline with flight * Update SimBriefController.php * Update SimBriefController.php --- .../Frontend/AirportController.php | 7 +++--- .../Frontend/DashboardController.php | 5 ++-- .../Controllers/Frontend/FlightController.php | 23 +++++++++++++++---- .../Controllers/Frontend/HomeController.php | 2 +- .../Controllers/Frontend/PirepController.php | 17 ++++++++++++-- .../Frontend/ProfileController.php | 9 +++----- .../Frontend/SimBriefController.php | 6 ++--- .../Controllers/Frontend/UserController.php | 6 ++++- app/Widgets/LatestNews.php | 2 +- app/Widgets/LatestPilots.php | 2 +- 10 files changed, 55 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/Frontend/AirportController.php b/app/Http/Controllers/Frontend/AirportController.php index 784ab0ee9..bc5a0cd1f 100644 --- a/app/Http/Controllers/Frontend/AirportController.php +++ b/app/Http/Controllers/Frontend/AirportController.php @@ -35,22 +35,23 @@ public function __construct( public function show($id, Request $request) { $id = strtoupper($id); + $with_flights = ['airline', 'arr_airport', 'dpt_airport']; - $airport = $this->airportRepo->where('id', $id)->first(); + $airport = $this->airportRepo->with('files')->where('id', $id)->first(); if (!$airport) { Flash::error('Airport not found!'); return redirect(route('frontend.dashboard.index')); } $inbound_flights = $this->flightRepo - ->with(['dpt_airport', 'arr_airport', 'airline']) + ->with($with_flights) ->findWhere([ 'arr_airport_id' => $id, 'active' => 1, ])->all(); $outbound_flights = $this->flightRepo - ->with(['dpt_airport', 'arr_airport', 'airline']) + ->with($with_flights) ->findWhere([ 'dpt_airport_id' => $id, 'active' => 1, diff --git a/app/Http/Controllers/Frontend/DashboardController.php b/app/Http/Controllers/Frontend/DashboardController.php index 8ab9ec375..dcae679c2 100644 --- a/app/Http/Controllers/Frontend/DashboardController.php +++ b/app/Http/Controllers/Frontend/DashboardController.php @@ -28,14 +28,15 @@ public function __construct(PirepRepository $pirepRepo) */ public function index() { - //dd(config('backup')); $last_pirep = null; + $with_pirep = ['aircraft', 'arr_airport', 'comments', 'dpt_airport']; /** @var \App\Models\User $user */ $user = Auth::user(); + $user->loadMissing('journal'); try { - $last_pirep = $this->pirepRepo->find($user->last_pirep_id); + $last_pirep = $this->pirepRepo->with($with_pirep)->find($user->last_pirep_id); } catch (\Exception $e) { } diff --git a/app/Http/Controllers/Frontend/FlightController.php b/app/Http/Controllers/Frontend/FlightController.php index 370e7fbc2..2db26ddda 100644 --- a/app/Http/Controllers/Frontend/FlightController.php +++ b/app/Http/Controllers/Frontend/FlightController.php @@ -88,6 +88,7 @@ public function search(Request $request) /** @var \App\Models\User $user */ $user = Auth::user(); + $user->loadMissing('current_airport'); if (setting('pilots.restrict_to_company')) { $where['airline_id'] = $user->airline_id; @@ -127,9 +128,11 @@ public function search(Request $request) $flights = $this->flightRepo->searchCriteria($request) ->with([ - 'dpt_airport', - 'arr_airport', 'airline', + 'alt_airport', + 'arr_airport', + 'dpt_airport', + 'subfleets.airline', 'simbrief' => function ($query) use ($user) { $query->where('user_id', $user->id); }, ]) @@ -215,7 +218,19 @@ public function bids(Request $request) */ public function show($id) { - $flight = $this->flightRepo->find($id); + $user_id = Auth::id(); + $with_flight = [ + 'airline', + 'alt_airport', + 'arr_airport', + 'dpt_airport', + 'subfleets.airline', + 'simbrief' => function ($query) use ($user_id) { + $query->where('user_id', $user_id); + }, + ]; + + $flight = $this->flightRepo->with($with_flight)->find($id); if (empty($flight)) { Flash::error('Flight not found!'); return redirect(route('frontend.dashboard.index')); @@ -224,7 +239,7 @@ public function show($id) $map_features = $this->geoSvc->flightGeoJson($flight); // See if the user has a bid for this flight - $bid = Bid::where(['user_id' => Auth::id(), 'flight_id' => $flight->id])->first(); + $bid = Bid::where(['user_id' => $user_id, 'flight_id' => $flight->id])->first(); return view('flights.show', [ 'flight' => $flight, diff --git a/app/Http/Controllers/Frontend/HomeController.php b/app/Http/Controllers/Frontend/HomeController.php index 4cec4b3b0..b761a583e 100644 --- a/app/Http/Controllers/Frontend/HomeController.php +++ b/app/Http/Controllers/Frontend/HomeController.php @@ -16,7 +16,7 @@ class HomeController extends Controller public function index() { try { - $users = User::where('state', '!=', UserState::DELETED)->orderBy('created_at', 'desc')->take(4)->get(); + $users = User::with('home_airport')->where('state', '!=', UserState::DELETED)->orderBy('created_at', 'desc')->take(4)->get(); } catch (\PDOException $e) { Log::emergency($e); return view('system/errors/database_error', [ diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 12f8e19bd..9240c70af 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -182,7 +182,7 @@ public function index(Request $request) $where = [['user_id', $user->id]]; $where[] = ['state', '<>', PirepState::CANCELLED]; - $with = ['airline', 'aircraft', 'dpt_airport', 'arr_airport', 'fares', 'comments']; + $with = ['aircraft', 'airline', 'arr_airport', 'comments', 'dpt_airport']; $this->pirepRepo->with($with) ->pushCriteria(new WhereCriteria($request, $where)); @@ -201,7 +201,20 @@ public function index(Request $request) */ public function show($id) { - $pirep = $this->pirepRepo->with(['simbrief'])->find($id); + $with = [ + 'acars_logs', + 'aircraft.airline', + 'airline.journal', + 'arr_airport', + 'comments', + 'dpt_airport', + 'fares.fare', + 'transactions', + 'simbrief', + 'user.rank', + ]; + + $pirep = $this->pirepRepo->with($with)->find($id); if (empty($pirep)) { Flash::error('Pirep not found'); return redirect(route('frontend.pirep.index')); diff --git a/app/Http/Controllers/Frontend/ProfileController.php b/app/Http/Controllers/Frontend/ProfileController.php index 4ba55412e..a08ead38a 100644 --- a/app/Http/Controllers/Frontend/ProfileController.php +++ b/app/Http/Controllers/Frontend/ProfileController.php @@ -80,9 +80,8 @@ public function index() public function show($id) { /** @var \App\Models\User $user */ - $user = User::with('airline', 'awards', 'current_airport', 'fields.field', 'home_airport', 'last_pirep', 'rank') - ->where('id', $id) - ->first(); + $with = ['airline', 'awards', 'current_airport', 'fields.field', 'home_airport', 'last_pirep', 'rank']; + $user = User::with($with)->where('id', $id)->first(); if (empty($user)) { Flash::error('User not found!'); @@ -110,9 +109,7 @@ public function show($id) public function edit(Request $request) { /** @var \App\Models\User $user */ - $user = User::with(['fields', 'fields.field']) - ->where('id', Auth::user()->id) - ->first(); + $user = User::with('fields.field')->where('id', Auth::id())->first(); if (empty($user)) { Flash::error('User not found!'); diff --git a/app/Http/Controllers/Frontend/SimBriefController.php b/app/Http/Controllers/Frontend/SimBriefController.php index 71291f440..2a6c21817 100644 --- a/app/Http/Controllers/Frontend/SimBriefController.php +++ b/app/Http/Controllers/Frontend/SimBriefController.php @@ -62,7 +62,7 @@ public function generate(Request $request) $aircraft_id = $request->input('aircraft_id'); /** @var Flight $flight */ - $flight = $this->flightRepo->with(['fares', 'subfleets'])->find($flight_id); + $flight = $this->flightRepo->with(['airline', 'arr_airport', 'dpt_airport', 'fares.fare', 'subfleets'])->find($flight_id); if (!$flight) { flash()->error('Unknown flight'); @@ -136,7 +136,7 @@ public function generate(Request $request) // SimBrief profile does not exists and everything else is ok // Select aircraft which will be used for calculations and details /** @var Aircraft $aircraft */ - $aircraft = Aircraft::where('id', $aircraft_id)->first(); + $aircraft = Aircraft::with(['airline'])->where('id', $aircraft_id)->first(); // Figure out the proper fares to use for this flight/aircraft $all_fares = $this->fareSvc->getFareWithOverrides($aircraft->subfleet->fares, $flight->fares); @@ -263,7 +263,7 @@ public function briefing($id) $user = Auth::user(); /** @var SimBrief $simbrief */ - $simbrief = SimBrief::with(['flight'])->find($id); + $simbrief = SimBrief::with(['flight.airline', 'pirep.airline'])->find($id); if (!$simbrief) { flash()->error('SimBrief briefing not found'); return redirect(route('frontend.flights.index')); diff --git a/app/Http/Controllers/Frontend/UserController.php b/app/Http/Controllers/Frontend/UserController.php index bb8303f02..d0237a1eb 100644 --- a/app/Http/Controllers/Frontend/UserController.php +++ b/app/Http/Controllers/Frontend/UserController.php @@ -30,6 +30,9 @@ public function __construct(UserRepository $userRepo) */ public function index(Request $request) { + $with = ['airline', 'current_airport', 'fields.field', 'home_airport', 'rank']; + $with_count = ['awards']; + $where = []; if (setting('pilots.hide_inactive')) { @@ -43,7 +46,8 @@ public function index(Request $request) } $users = $this->userRepo - ->with(['airline', 'current_airport']) + ->withCount($with_count) + ->with($with) ->orderBy('pilot_id', 'asc') ->paginate(); diff --git a/app/Widgets/LatestNews.php b/app/Widgets/LatestNews.php index 429b535ad..5c5d523a8 100644 --- a/app/Widgets/LatestNews.php +++ b/app/Widgets/LatestNews.php @@ -23,7 +23,7 @@ public function run() return view('widgets.latest_news', [ 'config' => $this->config, - 'news' => $newsRepo->recent($this->config['count']), + 'news' => $newsRepo->with('user')->recent($this->config['count']), ]); } } diff --git a/app/Widgets/LatestPilots.php b/app/Widgets/LatestPilots.php index d3b479f35..f49dfa461 100644 --- a/app/Widgets/LatestPilots.php +++ b/app/Widgets/LatestPilots.php @@ -21,7 +21,7 @@ class LatestPilots extends Widget public function run() { $userRepo = app(UserRepository::class); - $userRepo = $userRepo->where('state', '!=', UserState::DELETED)->orderby('created_at', 'desc')->take($this->config['count'])->get(); + $userRepo = $userRepo->with('home_airport')->where('state', '!=', UserState::DELETED)->orderby('created_at', 'desc')->take($this->config['count'])->get(); return view('widgets.latest_pilots', [ 'config' => $this->config,