Php 如何使可用的包含在HPLeague/fractal中工作
我无法实现分形包含。我试图包括一个特定用户的帖子 当我在UserItemTransformer顶部的$defaultIncludes中添加“posts”时,一切都很顺利。员额按预期列入。 但是,当我将$defaultIncludes更改为$availableIncludes时,即使在调用Php 如何使可用的包含在HPLeague/fractal中工作,php,laravel,laravel-5,thephpleague,thephpleague-fractal,Php,Laravel,Laravel 5,Thephpleague,Thephpleague Fractal,我无法实现分形包含。我试图包括一个特定用户的帖子 当我在UserItemTransformer顶部的$defaultIncludes中添加“posts”时,一切都很顺利。员额按预期列入。 但是,当我将$defaultIncludes更改为$availableIncludes时,即使在调用$fractal->parseIncludes('posts')之后,我的json输出中也不包括帖子 问题似乎在于,只有在我使用$defaultIncludes时才调用包含posts的方法。当我使用$availa
$fractal->parseIncludes('posts')之后,我的json输出中也不包括帖子代码>
问题似乎在于,只有在我使用$defaultIncludes时才调用包含posts的方法。当我使用$availableIncludes时,从未调用它
我可能错过了一些明显的东西。你能帮我找出是什么吗
这项工作:
// [...] Class UserItemTransformer
protected $defaultIncludes = [
'posts'
];
这不起作用:
// [...] Class UserItemTransformer
protected $availableIncludes = [
'posts'
];
// [...] Class PostsController
// $fractal is injected in the method (Laravel 5 style)
$fractal->parseIncludes('posts');
明白了
当我调用parseIncludes('posts')时,这是一个新的分形实例,注入到控制器方法中。当然,我应该在执行实际解析的分形实例上调用parseIncludes()(并且我在其他地方注入了一个Api类)
我现在就去坐在角落里休息一会儿。请将您的答案标记为正确,这样问题就不会显得没有答案。谢谢您的回复!我需要他:)
public function postsWithUser($user_id, Manager $fractal, UserRepositoryInterface $userRepository)
{
$api = new \App\Services\Api();
$user = $userRepository->findById($user_id);
if ( ! $user) {
return $api->errorNotFound();
}
$params = [
'offset' => $api->getOffset(),
'limit' => $api->getLimit()
];
$user->posts = $this->postRepository->findWithUser($user_id, $params);
// It used to be this, using $fractal, that I injected as method parameter
// I can now also remove the injected Manager $fractal from this method
// $fractal->parseIncludes('posts');
// I created a new getFractal() method on my Api class, that gives me the proper Fractal instance
$api->getFractal()->parseIncludes('posts');
return $api->respondWithItem($user, new UserItemTransformer());
}