Convert flight transformer to the native Laravel resource object

This commit is contained in:
Nabeel Shahzad
2017-12-12 07:25:11 -06:00
parent 2e1c0a75d4
commit 373f45c86c
9 changed files with 76 additions and 87 deletions

View File

@@ -7,27 +7,33 @@ use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use App\Models\Transformers\FlightTransformer;
use App\Repositories\FlightRepository;
use App\Http\Resources\Flight as FlightResource;
use Prettus\Repository\Exceptions\RepositoryException;
class FlightController extends AppBaseController
{
protected $flightRepo;
public function __construct(
FlightRepository $flightRepo
) {
public function __construct(FlightRepository $flightRepo) {
$this->flightRepo = $flightRepo;
}
public function get($id)
{
$flight = $this->flightRepo->find($id);
return fractal($flight, new FlightTransformer())->respond();
FlightResource::withoutWrapping();
return new FlightResource($flight);
}
public function search(Request $request)
{
$flights = $this->flightRepo->searchCriteria($request)->paginate();
return fractal($flights, new FlightTransformer())->respond();
try {
$flights = $this->flightRepo->searchCriteria($request)->paginate();
} catch (RepositoryException $e) {
return response($e, 503);
}
return FlightResource::collection($flights);
//return fractal($flights, new FlightTransformer())->respond();
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Flight extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'airline' => $this->airline,
'flight_number' => $this->flight_number,
'route_code' => $this->route_code,
'route_leg' => $this->route_leg,
'dpt_airport_id' => $this->dpt_airport_id,
'arr_airport_id' => $this->arr_airport_id,
'alt_airport_id' => $this->alt_airport_id,
'route' => $this->route,
'dpt_time' => $this->dpt_time,
'arr_time' => $this->arr_time,
'flight_time' => $this->flight_time,
'notes' => $this->notes,
'active' => $this->active,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}