Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an argument to fractal transformer transform method

Tags:

rest

php

api

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

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());
like image 511
MrFoh Avatar asked Mar 03 '15 07:03

MrFoh


Video Answer


1 Answers

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]))
like image 172
Michelangelo Avatar answered Oct 17 '22 02:10

Michelangelo