flugg / laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
MIT License
861 stars 86 forks source link

Transformer mappings circular dependencies #171

Open mauriciovillam opened 3 years ago

mauriciovillam commented 3 years ago

Hey there, thanks for this awesome package. This particular issue has been mentioned and fixed previously in #106. I've got this:

class VariationTransformer extends Transformer
{
    protected $load = [
        'product' => ProductTransformer::class
    ];

    public function transform(Variation $model): array
    {
        return [
            'id' => $model->id,
            'sku' => $model->sku,
            'name' => $model->name
        ];
    }
}

And this.

class ProductTransformer extends Transformer
{
    protected $load = [
        'variations' => VariationTransformer::class
    ];

    public function transform(Product $model): array
    {
        return [
            'name' => $model->name,
            'description' => $model->description
        ];
    }
}

The problem seems to be caused by the $load relationships calling each other. I'm using the latest version (3.1.2).