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
This commit is contained in:
B.Fatih KOZ
2021-11-09 17:51:02 +03:00
committed by GitHub
parent 2dbe19fdcc
commit 4c60e5da69
10 changed files with 55 additions and 24 deletions

View File

@@ -35,22 +35,23 @@ class AirportController extends Controller
public function show($id, Request $request) public function show($id, Request $request)
{ {
$id = strtoupper($id); $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) { if (!$airport) {
Flash::error('Airport not found!'); Flash::error('Airport not found!');
return redirect(route('frontend.dashboard.index')); return redirect(route('frontend.dashboard.index'));
} }
$inbound_flights = $this->flightRepo $inbound_flights = $this->flightRepo
->with(['dpt_airport', 'arr_airport', 'airline']) ->with($with_flights)
->findWhere([ ->findWhere([
'arr_airport_id' => $id, 'arr_airport_id' => $id,
'active' => 1, 'active' => 1,
])->all(); ])->all();
$outbound_flights = $this->flightRepo $outbound_flights = $this->flightRepo
->with(['dpt_airport', 'arr_airport', 'airline']) ->with($with_flights)
->findWhere([ ->findWhere([
'dpt_airport_id' => $id, 'dpt_airport_id' => $id,
'active' => 1, 'active' => 1,

View File

@@ -28,14 +28,15 @@ class DashboardController extends Controller
*/ */
public function index() public function index()
{ {
//dd(config('backup'));
$last_pirep = null; $last_pirep = null;
$with_pirep = ['aircraft', 'arr_airport', 'comments', 'dpt_airport'];
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = Auth::user(); $user = Auth::user();
$user->loadMissing('journal');
try { 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) { } catch (\Exception $e) {
} }

View File

@@ -88,6 +88,7 @@ class FlightController extends Controller
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = Auth::user(); $user = Auth::user();
$user->loadMissing('current_airport');
if (setting('pilots.restrict_to_company')) { if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = $user->airline_id; $where['airline_id'] = $user->airline_id;
@@ -127,9 +128,11 @@ class FlightController extends Controller
$flights = $this->flightRepo->searchCriteria($request) $flights = $this->flightRepo->searchCriteria($request)
->with([ ->with([
'dpt_airport',
'arr_airport',
'airline', 'airline',
'alt_airport',
'arr_airport',
'dpt_airport',
'subfleets.airline',
'simbrief' => function ($query) use ($user) { 'simbrief' => function ($query) use ($user) {
$query->where('user_id', $user->id); $query->where('user_id', $user->id);
}, ]) }, ])
@@ -215,7 +218,19 @@ class FlightController extends Controller
*/ */
public function show($id) 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)) { if (empty($flight)) {
Flash::error('Flight not found!'); Flash::error('Flight not found!');
return redirect(route('frontend.dashboard.index')); return redirect(route('frontend.dashboard.index'));
@@ -224,7 +239,7 @@ class FlightController extends Controller
$map_features = $this->geoSvc->flightGeoJson($flight); $map_features = $this->geoSvc->flightGeoJson($flight);
// See if the user has a bid for this 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', [ return view('flights.show', [
'flight' => $flight, 'flight' => $flight,

View File

@@ -16,7 +16,7 @@ class HomeController extends Controller
public function index() public function index()
{ {
try { 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) { } catch (\PDOException $e) {
Log::emergency($e); Log::emergency($e);
return view('system/errors/database_error', [ return view('system/errors/database_error', [

View File

@@ -182,7 +182,7 @@ class PirepController extends Controller
$where = [['user_id', $user->id]]; $where = [['user_id', $user->id]];
$where[] = ['state', '<>', PirepState::CANCELLED]; $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) $this->pirepRepo->with($with)
->pushCriteria(new WhereCriteria($request, $where)); ->pushCriteria(new WhereCriteria($request, $where));
@@ -201,7 +201,20 @@ class PirepController extends Controller
*/ */
public function show($id) 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)) { if (empty($pirep)) {
Flash::error('Pirep not found'); Flash::error('Pirep not found');
return redirect(route('frontend.pirep.index')); return redirect(route('frontend.pirep.index'));

View File

@@ -80,9 +80,8 @@ class ProfileController extends Controller
public function show($id) public function show($id)
{ {
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = User::with('airline', 'awards', 'current_airport', 'fields.field', 'home_airport', 'last_pirep', 'rank') $with = ['airline', 'awards', 'current_airport', 'fields.field', 'home_airport', 'last_pirep', 'rank'];
->where('id', $id) $user = User::with($with)->where('id', $id)->first();
->first();
if (empty($user)) { if (empty($user)) {
Flash::error('User not found!'); Flash::error('User not found!');
@@ -110,9 +109,7 @@ class ProfileController extends Controller
public function edit(Request $request) public function edit(Request $request)
{ {
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = User::with(['fields', 'fields.field']) $user = User::with('fields.field')->where('id', Auth::id())->first();
->where('id', Auth::user()->id)
->first();
if (empty($user)) { if (empty($user)) {
Flash::error('User not found!'); Flash::error('User not found!');

View File

@@ -62,7 +62,7 @@ class SimBriefController
$aircraft_id = $request->input('aircraft_id'); $aircraft_id = $request->input('aircraft_id');
/** @var Flight $flight */ /** @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) { if (!$flight) {
flash()->error('Unknown flight'); flash()->error('Unknown flight');
@@ -136,7 +136,7 @@ class SimBriefController
// SimBrief profile does not exists and everything else is ok // SimBrief profile does not exists and everything else is ok
// Select aircraft which will be used for calculations and details // Select aircraft which will be used for calculations and details
/** @var Aircraft $aircraft */ /** @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 // Figure out the proper fares to use for this flight/aircraft
$all_fares = $this->fareSvc->getFareWithOverrides($aircraft->subfleet->fares, $flight->fares); $all_fares = $this->fareSvc->getFareWithOverrides($aircraft->subfleet->fares, $flight->fares);
@@ -263,7 +263,7 @@ class SimBriefController
$user = Auth::user(); $user = Auth::user();
/** @var SimBrief $simbrief */ /** @var SimBrief $simbrief */
$simbrief = SimBrief::with(['flight'])->find($id); $simbrief = SimBrief::with(['flight.airline', 'pirep.airline'])->find($id);
if (!$simbrief) { if (!$simbrief) {
flash()->error('SimBrief briefing not found'); flash()->error('SimBrief briefing not found');
return redirect(route('frontend.flights.index')); return redirect(route('frontend.flights.index'));

View File

@@ -30,6 +30,9 @@ class UserController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$with = ['airline', 'current_airport', 'fields.field', 'home_airport', 'rank'];
$with_count = ['awards'];
$where = []; $where = [];
if (setting('pilots.hide_inactive')) { if (setting('pilots.hide_inactive')) {
@@ -43,7 +46,8 @@ class UserController extends Controller
} }
$users = $this->userRepo $users = $this->userRepo
->with(['airline', 'current_airport']) ->withCount($with_count)
->with($with)
->orderBy('pilot_id', 'asc') ->orderBy('pilot_id', 'asc')
->paginate(); ->paginate();

View File

@@ -23,7 +23,7 @@ class LatestNews extends Widget
return view('widgets.latest_news', [ return view('widgets.latest_news', [
'config' => $this->config, 'config' => $this->config,
'news' => $newsRepo->recent($this->config['count']), 'news' => $newsRepo->with('user')->recent($this->config['count']),
]); ]);
} }
} }

View File

@@ -21,7 +21,7 @@ class LatestPilots extends Widget
public function run() public function run()
{ {
$userRepo = app(UserRepository::class); $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', [ return view('widgets.latest_pilots', [
'config' => $this->config, 'config' => $this->config,