您可以将额外的参数传递给转换器构造函数。
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 ]; } }
然后打电话
return $this->response->paginator($rows, new SymptomTransformer(['user_id' => $user_id]));