本文介绍了将参数传递给分形变压器变换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am使用来自phpleague的分形软件包.我有这样的转换类设置

Am using fractal package from phpleague. I have a transform class setup like this

    class ConversationTransformer extends TransformerAbstract
    {

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

但是当我尝试访问它时,我却缺少变换的参数2异常

however i get missing argument 2 exception for transform when i try to access it

$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 [];
    }
}

通过以下返回方式调用它:

Call it with this return:

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

这篇关于将参数传递给分形变压器变换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:53