chrisbjr / api-guard

A simple way of authenticating your RESTful APIs with API keys using Laravel
MIT License
692 stars 148 forks source link

ApiGuard #135

Open JonathanFontesMonday opened 8 years ago

JonathanFontesMonday commented 8 years ago

Hello,

I have an issue with transform and availableIncludes.

This is my LeagueTransformer.php

<?php
...
/**
     * List of resources possible to include
     *
     * @var array
     */
    protected $availableIncludes = [
        'teams'
    ];

    public function transform(League $league)
    {
        return [
            'id'        => (int) $league->id,
            'name'      => (string) $league->name,
            'slug_name' => (string) $league->slug_name,
            'nice_name' => (string) $league->nice_name,
            'image_logo' => (string) $league->image_logo_league,
            'active_year' => (string) $league->year,
            'country_code' => (string) $league->country_code
        ];
    }

    /**
     * Include Teams
     *
     * @return League\Fractal\ItemResource
     */
    public function includeTeams(League $league)
    {
        // here i have my problem  
        $teams = $league->teams();

        return $this->collection($teams, new TeamsTransformer(), 'teams');
    }

When i do, $leagues->teams()->count() i got, 30 rows, but this never passed out to response. And yes, i put on my query string ?include=teams, but nothing. This is the result,

{
  "data": [
    {
      "id": 1,
      "name": "Argentina - Primera Division",
      "slug_name": "",
      "nice_name": "Argentina - Primera Division",
      "image_logo": "http://thumb.resfu.com/media/img/league_logos/primera_argentina.png?size=120x&ext=png&lossy=1&1",
      "active_year": "2016",
      "country_code": "AR",
      "teams": {
        "data": []
      }
    }
  ]
}
johannesschobel commented 5 years ago

you need to call $teams = $league->teams; (without the ()). You want to pass a collection to the $this->collection() method and not a Builder instance!

Cheers