Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Transformer in one to many relationship. #1054

Closed
MicoleChan opened this issue Jun 6, 2016 · 1 comment
Closed

How to use Transformer in one to many relationship. #1054

MicoleChan opened this issue Jun 6, 2016 · 1 comment

Comments

@MicoleChan
Copy link

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.

@MicoleChan
Copy link
Author

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);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant