list trips
add, remove flights
This commit is contained in:
26
app/Libraries/Trips/FlightTransform.php
Normal file
26
app/Libraries/Trips/FlightTransform.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace App\Libraries\Trips;
|
||||
|
||||
use App\Libraries\Trips\Models\Flights;
|
||||
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class FlightTransform extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Turn this item object into a generic array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Flights $flight)
|
||||
{
|
||||
return [
|
||||
'id' => $flight->id,
|
||||
'name' => $flight->destination,
|
||||
'links' => [
|
||||
'rel' => 'self',
|
||||
'uri' => '/flights/'.$flight->id,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
24
app/Libraries/Trips/Models/Flights.php
Normal file
24
app/Libraries/Trips/Models/Flights.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\Trips\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Flights extends Model
|
||||
{
|
||||
|
||||
protected $fillable = ['destination'];
|
||||
|
||||
public function trips()
|
||||
{
|
||||
return $this->belongsToMany(Trips::class, 'trip_flights');
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'destination' => $this->destination,
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Libraries/Trips/Models/Trips.php
Normal file
19
app/Libraries/Trips/Models/Trips.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\Trips\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Trips extends Model
|
||||
{
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
/**
|
||||
* relation to flights
|
||||
*/
|
||||
public function flights()
|
||||
{
|
||||
return $this->belongsToMany(Flights::class, 'trip_flights');
|
||||
}
|
||||
}
|
||||
49
app/Libraries/Trips/TripTransformer.php
Normal file
49
app/Libraries/Trips/TripTransformer.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace App\Libraries\Trips;
|
||||
|
||||
use App\Libraries\Trips\Models\Flights;
|
||||
use App\Libraries\Trips\Models\Trips;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class TripTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* List of resources possible to include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = [
|
||||
'flights'
|
||||
];
|
||||
|
||||
/**
|
||||
* Turn this item object into a generic array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Trips $trip)
|
||||
{
|
||||
return [
|
||||
'id' => $trip->id,
|
||||
'name' => $trip->name,
|
||||
'updated_at' => $trip->updated_at,
|
||||
'flights' => $trip->flights()->get(),
|
||||
'links' => [
|
||||
'rel' => 'self',
|
||||
'uri' => '/trips/'.$trip->code,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Include Author
|
||||
*
|
||||
* @return \League\Fractal\Resource\ResourceAbstract
|
||||
*/
|
||||
public function includeFlights(Trips $trip)
|
||||
{
|
||||
$flights = $trip->flights()->get();
|
||||
|
||||
return $this->collection($flights, new FlightTransform());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user