title: How to make Laravel resources when multiple models in output
body: <p>I'm building a Laravel API. I have two models: Listings and Reservations.</p>
  
  <pre><code>class Listing extends Model
  {
      public function reservations()
      {
          return $this-&gt;hasMany(Reservation::class);
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Reservation extends Model
  {
      public function listing()
      {
          return $this-&gt;belongsTo(Listing::class);
      }
  </code></pre>
  
  <p>I have also created two Laravel resources: </p>
  
  <pre><code>class ListingResource extends JsonResource
      {
          public function toArray($request)
          {
              return [
                  'id'            =&gt;  $this-&gt;id,
                  'user_id'       =&gt;  $this-&gt;user-&gt;id,
                  'name'          =&gt;  $this-&gt;name,
                  'description'   =&gt;  $this-&gt;description,
                  'price'         =&gt;  $this-&gt;price,
              ];
          }
      }
  </code></pre>
  
  <p>I have also defined Laravel Resources:</p>
  
  <pre><code>class ReservationResource extends JsonResource
  {
      public function toArray($request)
      {
          return [
              'id'            =&gt;  $this-&gt;id,
              'listing_id'    =&gt;  $this-&gt;listing-&gt;id,
              'start_date'    =&gt;  $this-&gt;start_date,
              'end_date'      =&gt;  $this-&gt;end_date,
              'price'         =&gt;  $this-&gt;price,
              'total'         =&gt;  $this-&gt;total,
              'listing'       =&gt;  $this-&gt;listing,
          ];
      }
  }
  </code></pre>
  
  <p>I want to retrieve all reservations belonging to the current logged in user. I have the following in my ReservationsController.</p>
  
  <pre><code>class ReservationController extends Controller
  {
      public function getUserReservations()
      {
          $currentUser = JWTAuth::user()-&gt;id;
          $listings = Listing::where('user_id', $currentUser)-&gt;get('id');
          $reservations = Reservation::with('Listing')-&gt;whereIn('listing_id', $listings)-&gt;paginate(25);
  
          return response()-&gt;json([
              'data' =&gt; $reservations
          ]);
      }
  
      public function getAllReservations()
      {
          return ReservationResource::collection(Reservation::paginate(25));
       }
  </code></pre>
  
  <p>For the method <code>getAllReservations</code> I'm using resources.  For the method <code>getUserReservations</code> I used a different methodology. This works but I would like to keep my code and API as consistent as possible and hence also use ReservationResource for the <code>getUserReservations</code>.</p>
  
  <p>How can I convert the code for <code>getUserReservations</code> in the controller to make use of Laravel resources.</p>