From 9ed2e3f9f4617f2ad76cb44a1a2d5139f8d935b2 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Wed, 4 Mar 2020 18:14:03 -0500 Subject: [PATCH] API: Flight fields are an array when empty #618 (#619) * Ensure 'fields' field is return as an object for flights #618 * Eager loading for flight fields --- app/Http/Controllers/Api/FlightController.php | 10 +++++++++- app/Http/Resources/Flight.php | 11 +++++++++-- resources/views/admin/flights/fields.blade.php | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index 070d8176..9bcc89b6 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -88,7 +88,15 @@ class FlightController extends Controller $this->flightRepo->pushCriteria(new WhereCriteria($request, $where)); $this->flightRepo->pushCriteria(new RequestCriteria($request)); - $flights = $this->flightRepo->paginate(); + $flights = $this->flightRepo + ->with([ + 'airline', + 'subfleets', + 'subfleets.aircraft', + 'subfleets.fares', + 'field_values', + ]) + ->paginate(); } catch (RepositoryException $e) { return response($e, 503); } diff --git a/app/Http/Resources/Flight.php b/app/Http/Resources/Flight.php index c18918d7..684e9e42 100644 --- a/app/Http/Resources/Flight.php +++ b/app/Http/Resources/Flight.php @@ -3,18 +3,25 @@ namespace App\Http\Resources; use App\Support\Units\Distance; +use stdClass; class Flight extends Response { /** * Set the fields on the flight object * - * @return array + * @mixin \App\Models\Flight */ private function setFields() { + /** @var \Illuminate\Support\Collection $field_values */ + $field_values = $this->field_values; + if (empty($field_values) || $field_values->count() === 0) { + return new stdClass(); + } + $fields = []; - foreach ($this->field_values as $field) { + foreach ($field_values as $field) { $fields[$field->name] = $field->value; } diff --git a/resources/views/admin/flights/fields.blade.php b/resources/views/admin/flights/fields.blade.php index 2efd88d6..59ffdfb6 100644 --- a/resources/views/admin/flights/fields.blade.php +++ b/resources/views/admin/flights/fields.blade.php @@ -235,7 +235,7 @@