Laravel Fractal变换器,如何通过并获得额外的变量

I'm using Dingo API to create an API in Laravel 5.2 and have a controller returning data with

 return $this->response->paginator($rows, new SymptomTransformer, ['user_id' => $user_id]);

However, I don't know how to retrieve user_id value in the SymptomTransformer! Tried many different ways and tried looking into the class but I'm relatively new to both Laravel and OOP so if anyone can point me to the right direction, it'd be greatly appreciated.

Below is my transformer class.

class SymptomTransformer extends TransformerAbstract
{
    public function transform(Symptom $row)
    {  
        // need to get user_id here
        return [
            'id' => $row->id,
            'name' => $row->name,
            'next_type' => $next,
            'allow' => $allow
        ];
    }
}

You can pass extra parameter to transformer constructor.

class SymptomTransformer extends TransformerAbstract
{
     protected $extra;

     public function __construct($extra) {
         $this->extra = $exta;
     }

      public function transform(Symptom $row)
      {  
         // need to get user_id here
         dd($this->extra);
         return [
            'id' => $row->id,
            'name' => $row->name,
            'next_type' => $next,
            'allow' => $allow
        ];
      }
}

And call like

return $this->response->paginator($rows, new SymptomTransformer(['user_id' => $user_id]));

If you are using Dependency Injection, then you need to pass params afterwards.

This is my strategy:

<?php

namespace App\Traits;

trait TransformerParams {

    private $params;

    public function addParam() {
        $args = func_get_args();
        if(is_array($args[0]))
        {
            $this->params = $args[0];
        } else {
            $this->params[$args[0]] = $args[1];
        }
    }
}

Then you implement the trait in your transformer:

<?php
namespace App\Transformers;

use App\Traits\TransformerParams;
use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{

    use TransformerParams;

    public function transform(User $user)
    {
        return array_merge([
            'id' => (int) $user->id,
            'username' => $user->username,
            'email' => $user->email,
            'role' => $user->roles[0],
            'image' => $user->image
        ], $this->params); // in real world, you'd not be using array_merge
    }
}

So, in your Controller, just do this:

public function index(Request $request, UserTransformer $transformer)
{
    $transformer->addParam('has_extra_param', ':D');
    // ... rest of the code
}

Basically, the trait is a bag for extra params.

You can set extra param via setter.

class SymptomTransformer extends TransformerAbstract
{
    public function transform(Symptom $row)
    {  
        // need to get user_id here
        dd($this->test_param);
        return [
            'id' => $row->id,
            'name' => $row->name,
            'next_type' => $next,
            'allow' => $allow
        ];
    }
    public function setTestParam($test_param)
    {
        $this->test_param = $test_param;
    }
}

And then:

$symptomTransformer = new SymptomTransformer;
$symptomTransformer->setTestParam('something');
return $this->response->paginator($rows, $symptomTransformer);