我正在尝试使用变换器返回响应数据,但它返回错误
类型错误:传递给App \ Support \ Response :: collection()的参数2必须是League \ Fractal \ TransformerAbstract的实例,App \ Transformers \ UserTransformer的实例给出,
在我打印$user
时,在登录控制器中打印数据
[original:protected] => Array
(
[id] => 2
[email] => test@test.com
[password] => $2y$10$fyORQUSfUpIhDjrIBD2TK.elGKwdzV10YOmLd9Goks3z52AUYi8mK
[status] => Active
[system_role] => User
[parent_user_id] => 0
[otp_verifiy_id] => 253526851
[last_login] => Carbon\Carbon Object
(
[date] => 2018-04-24 15:49:22.659941
[timezone_type] => 3
[timezone] => Asia/Calcutta
)
[remember_token] => eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vMTkyLjE2OC41LjE1ODo4MDAwL2FwaS9hdXRoL2xvZ2l
[created_at] => 2018-04-20 14:47:27
[updated_at] => 2018-04-24 15:49:22
)
之后,我将$user
转换为UserTransformer
print_r($user);die; // above data
return $this->response->collection($user, new UserTransformer);
在我的UserTransformer
代码中看起来像
<?php
namespace App\Transformers;
use App\User;
use League\Fractal\TransformerAbstract;
use Illuminate\Http\Request;
class UserTransformer {
public function transform(User $user) {
print_r($user);die;
}
}
当UserTransformer
打印$user
时,它会返回错误。我做错了什么。?
答案 0 :(得分:1)
错误在这里:
return $this->response->collection($user, new UserTransformer);
new UserTransformer
是App\Transformers\UserTransformer
的实例,但应该是League\Fractal\TransformerAbstract
的实例
您确定类UserTransformer
不应该实现任何接口吗?
答案 1 :(得分:0)
您需要在use GuzzleHttp\Psr7\Response;
$responses = [];
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com'
]);
$requests = function () use ($client) {
for ($i = 0; $i < 2000; $i++) {
yield function() use ($client,$i) {
return $client->postAsync('/api/book?id=' . $i);
};
}
};
$pool = new \GuzzleHttp\Pool($client, $requests(),[
'concurrency' => 5,
'fulfilled' => function (Response $response, $index) use (&$responses) {
// if ($response->getStatusCode() == 200) {
$responses[] = json_decode($response->getBody(), true);
// }
print_r($responses); // this will have all the responses
},
'rejected' => function (\GuzzleHttp\Exception\RequestException $reason, $index) {
// dd($reason); //you can store it in laravel logs
},
]);
$pool->promise()->wait();
return response()->json($responses);
类中扩展TransformerAbstract
:
UserTransformer