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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
{
$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,

View File

@ -28,14 +28,15 @@ class DashboardController extends Controller
*/
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) {
}

View File

@ -88,6 +88,7 @@ class FlightController extends Controller
/** @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 @@ class FlightController extends Controller
$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 @@ class FlightController extends Controller
*/
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 @@ class FlightController extends Controller
$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,

View File

@ -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', [

View File

@ -182,7 +182,7 @@ class PirepController extends Controller
$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 @@ class PirepController extends Controller
*/
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'));

View File

@ -80,9 +80,8 @@ class ProfileController extends Controller
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 @@ class ProfileController extends Controller
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!');

View File

@ -62,7 +62,7 @@ class SimBriefController
$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 @@ class SimBriefController
// 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 @@ class SimBriefController
$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'));

View File

@ -30,6 +30,9 @@ class UserController extends Controller
*/
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 @@ class UserController extends Controller
}
$users = $this->userRepo
->with(['airline', 'current_airport'])
->withCount($with_count)
->with($with)
->orderBy('pilot_id', 'asc')
->paginate();

View File

@ -23,7 +23,7 @@ class LatestNews extends Widget
return view('widgets.latest_news', [
'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()
{
$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,