Skip to content

Commit

Permalink
Eager loading update for frontend controllers and widgets (#1348)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FatihKoz committed Nov 9, 2021
1 parent 2dbe19f commit 4c60e5d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 24 deletions.
7 changes: 4 additions & 3 deletions app/Http/Controllers/Frontend/AirportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Frontend/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand Down
23 changes: 19 additions & 4 deletions app/Http/Controllers/Frontend/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}, ])
Expand Down Expand Up @@ -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'));
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Frontend/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
17 changes: 15 additions & 2 deletions app/Http/Controllers/Frontend/PirepController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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'));
Expand Down
9 changes: 3 additions & 6 deletions app/Http/Controllers/Frontend/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down Expand Up @@ -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!');
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Frontend/SimBriefController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'));
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/Frontend/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion app/Widgets/LatestNews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
]);
}
}
2 changes: 1 addition & 1 deletion app/Widgets/LatestPilots.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4c60e5d

Please sign in to comment.