Api: How to use Transformer in one to many relationship.

Created on 6 Jun 2016  路  1Comment  路  Source: dingo/api

I have two models which are one to many relationship:

Reservation.php (Master)

<?php

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)

<?php

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.

Most helpful comment

According to the Fractal Transformer docs, I resolved this problem by adding the include method.

    protected $defaultIncludes = [
        'reservation_details'
    ];

    public function includeReservationDetails(Reservation $reservation)
    {
        $reservation_detail = $reservation->reservation_details;

        return $this->collection($reservation_detail, new ReservationDetailTransformer);
    }

>All comments

According to the Fractal Transformer docs, I resolved this problem by adding the include method.

    protected $defaultIncludes = [
        'reservation_details'
    ];

    public function includeReservationDetails(Reservation $reservation)
    {
        $reservation_detail = $reservation->reservation_details;

        return $this->collection($reservation_detail, new ReservationDetailTransformer);
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tankhit picture tankhit  路  3Comments

lloricode picture lloricode  路  3Comments

sukh-gill picture sukh-gill  路  3Comments

BartHuis picture BartHuis  路  3Comments

HTMHell picture HTMHell  路  4Comments