0

my codes are constructed by Laravel+dingo.

I have two models which are one to many relationship:

Reservation.php (Master)

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Reservation extends Model
{
    protected  $table = 'dbo.Reservation';

    public function hasManyReservationDetails()
    {
        return $this->hasMany('App\Models\ReservationDetail', 'ReservationID', 'ReservationID');
    }
}

ReservationDetail.php (Detail)

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ReservationDetail extends Model
{
    protected  $table = 'dbo.ReservationDetail';

    public function belongsToReservation()
    {
        return $this->belongsTo('App\Models\Reservation', 'ReservationID', 'ReservationDetailID');
    }

}

And two Transformers for the two models as following:

ReservationTransformer

public function transform(Reservation $reservation)
{
    return [
        'reservation_id'        => (int) $reservation->ReservationID,
        'reservation_no'        => $reservation->ReservationNo,
    ];
}

ReservationDetail Transformer

public function transform(ReservationDetail $reservation_detail)
{
    return [
        'reservation_detail_id' => (int) $reservation_detail->ReservationDetailID,
        'reservation_id'        => (int) $reservation_detail->ReservationID,
        'room_no'               => $reservation_detail->RoomNo,
    ];
}

My controller and inquire

    $reservation = Reservation::where('ReservationNo', '=', $reservation_no)
        ->with('ReservationDetails')
        ->get();
     return $reservation;

I get the following return

{
  "Reservations": [
    {
      "ReservationID": "1",
      "ReservationNo": "2016-06-01 16:50:59.0659",
      "reservation_details": [
        {
          "ReservationDetailID": "1",
          "ReservationID": "1",
          "RoomNo": "001",
        },
        {
          "ReservationDetailID": "2",
          "ReservationID": "1",
          "RoomNo": "002",
        }
      ]
    }
  ]
}

I try the following but only return the translation of master table.

$reservation = $this->collection($reservation, new ReservationTransformer());

**

How can I transform the the data of master and detail table together?

**

I am not really understand how 'Custom Transformation Layer' works, anyone who can give me an example?

Many Thanks.

1 Answers1

1

You can use fractals build in support for transformer includes. See http://fractal.thephpleague.com/transformers/

I would start by renaming public function hasManyReservationDetails() to public function reservationDetails().

And then your ReservationTransformer will take care of the rest:

use League\Fractal\TransformerAbstract;

class ReservationTransformer extends TransformerAbstract
{
    /**
     * List of resources to automatically include
     *
     * @var array
     */
    protected $defaultIncludes = [
        'reservationDetails',
    ];

    public function transform(Reservation $reservation)
    {
        return [
            'reservation_id'        => (int) $reservation->ReservationID,
            'reservation_no'        => $reservation->ReservationNo,
        ];
    }

    /**
     * Include ReservationDetail
     *
     * @param  Reservation  $reservation
     *
     * @return League\Fractal\Resource\Collection
     */
    public function includeReservationDetails(Reservation $reservation)
    {
        return $this->collection($reservation->reservationDetails, new ReservationDetailTransformer);
    }
}
Ralla
  • 168
  • 1
  • 9