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

Access loaded relations before those are loaded #156

Open Hesesses opened 4 years ago

Hesesses commented 4 years ago

Hi,

The title of the issue is superbad, but i try to explain it better here:

I have a http request, lets say GET /teams?with=players and later on my controller I have:

$teams = $teamsService->getTeams();
return responder()
->success($teams->paginate())
->respond(200);

I would like to use ->meta() to include something related to players:

$teams = $teamsService->getTeams();
return responder()
->success($teams->paginate())
->meta($teamsService->getPlayerMeta($teams))
->respond(200);

function getPlayerMeta($teams) {
   dd($teams); // this only have the array of teams without the ?with=players relation
}

The questions is, is it somehow possible to access the relations that are going to be loaded and use those in ->meta() ?

flugg commented 4 years ago

I'm afraid there's currently no way to do this as the relations are kind of hard coded into the last step of the response creation. Luckily this will be possible in the new version though, as all relations will have to be loaded before sending them to the responder.