class ProjectTransformer extends TransformerAbstract
{
protected $availableIncludes = [
'resource'
];
public function transform(Project $project)
{
return [
'id' => (int) $project->id,
'title' => $project->title,
'description' => $project->description,
'git_link' => $project->git_link,
'budget' => $project->budget,
'paid' => $project->paid,
'project_type' => $project->project_type,
'project_status' => $project->project_status,
];
}
public function includeResource(Project $project)
{
$resources = $project->resources;
return $this->collection($resources, ResourceTransformer::class);
}
}
what I have to do in my controller so that can i use this link "api/projects?include=resource" and get all the projects with the resourse?
My controller code is here :
$project = Project::all();
return $this->response->collection($project, ProjectTransformer::class, [], function($resource, $fractal){
if(isset($_GET['include'])){
$fractal->parseIncludes(explode(',', $_GET['include']));
}
});
getting this error
Call to undefined method IlluminateDatabaseQueryBuilder::resource()
@ratulcse27 The issue doesn't seem to be coming from Fractal's includes system, but with your Eloquent model itself.
@ratulcse27 I think you're issue might be related to this #986, which means Dingo is trying to eager load a relationship based on the include that doesnt exist on the class. If you rename the include to match the relationship, you should be good
@hskrasek thank you for guiding me in the right direction. It's working now. But one more question, https://github.com/dingo/api/issues/986 in this thread one of them told that
If the include key in the transformer is not different from the relationship function name, you can assign the key-value array in $defaultIncludes. The key is the relationship function name, the value is the include key name.
But for me this isn't working. Can you tell me is this possible with $availableIncludes.
Does using ::class instead of new ...Transformer actually work?
@Deleugpn Sorry, It doesn't work with ::class, I fixed it in my code but forget to fix it here.
@ratulcse27 That doesn't seem to be possible with Fractal. Fractal just takes the include from $availableIncludes once it confirms its actually available, and uses it to call the include function (can see that here).
@hskrasek I understand it now. Thanks for your help.