gpt4 book ai didi

php - 将参数传递给分形更改器(mutator)变换方法

转载 作者:可可西里 更新时间:2023-11-01 12:48:23 25 4
gpt4 key购买 nike

我正在使用 phpleague 的分形包。我有一个像这样的转换类设置

    class ConversationTransformer extends TransformerAbstract
{

public function transform (Conversation $conversation, $user)
{
return [];
}
}

然而,当我尝试访问它时,我得到了 missing argument 2 exception for transform

$user = $this->people->get($this->user());
//conversations
$conversations = $this->conversations->user($user);
return $this->fractal->paginatedCollection($conversations, $user, new ConversationTransformer());

最佳答案

class ConversationTransformer extends TransformerAbstract
{
private $params = [];

function __construct($params = [])
{
$this->params = $params;
}

public function transform (Conversation $conversation)
{
//-- $this->params will be used here

return [];
}
}

用这个返回调用它:

 return $this->fractal->paginatedCollection($conversations, new ConversationTransformer(['user' => $user]))

关于php - 将参数传递给分形更改器(mutator)变换方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28826615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com