Laravel Responsable - 获取输出属性

时间:2017-09-12 05:17:52

标签: laravel laravel-5

class TransformResponse implements Responsable
{
    public $name;

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

    public function toResponse($request)
    {
        $this->name = 'Mr. ' . $this->name;
        return response(['name' => $this->name]);
    }
}

-

$res = new \TransformResponse('Orocimaru');
return $res; // output { "name": "Mr. Orocimaru" }
return $res->name; // output "Orocimaru"
# expected output "Mr. Orochimaru"

-

如何获得预期的输出?

是否有权在toResponse()中进行突变?

我遵循此https://laravel-news.com/laravel-5-5-responsable

0 个答案:

没有答案