我有一个数组,如示例中所示。使用我编写的代码,我得到了一个类似于 Output1 中的输出。
问:我请求的服务与我自己的应用程序不同。答案是一个数组,它包含 id 信息。有权限 () 用于授权控制。权限函数将用户权限返回为真假。但是我无法编写正确的语法,因为我无法完全记住这个函数。当我使用自己的代码时,我可以拉取具有 output1 中的权限的用户。但我希望分页信息保留在数组中。如输出2所示。我应该应用什么方法?
此数组就是一个示例。我只是试图解释我自己的问题。
我的代码=>(拉拉维尔 5.8)
$response = ….get(url)………
$list = $response->getBody()[‘content’];
$collection = collect($list);
$filtered = $collection->filter(function ($item) [
return auth()->user->permission($item['id'])
]);
Return [‘content’ => $filtered];
原始响应:
[
"paginate"=>
[
"page"=> 5,
"per_page"=> 20,
"page_count"=> 20,
"total_count"=> 521,
"Links"=> [
["self"=> "/products?page=5&per_page=20"],
["first"=> "/products?page=0&per_page=20"],
]
],
"content"=> [
[
"id"=> 1,
"name"=> "Widget #1",
],
[
"id"=> 2,
"name"=> "Widget #2",
],
[
"id"=> 3,
"name"=> "Widget #3",
]
]
]
输出 1:
[
"content"=>
[
"id"=> 1,
"name"=> "Widget #1",
],
----------------------> Authorized users(id= 1, 3) here, but no pagination.
[
"id"=> 3,
"name"=> "Widget #3",
]
]
预期输出:
[
"paginate"=>
[
"page"=> 5,
"per_page"=> 20,
"page_count"=> 20,
"total_count"=> 521,
"Links"=> [
["self"=> "/products?page=5&per_page=20"],
["first"=> "/products?page=0&per_page=20"],
]
],
"content"=> [
[
"id"=> 1,
"name"=> "Widget #1",
"uri"=> "/products/1"
],
[
"id"=> 3,
"name"=> "Widget #3",
"uri"=> "/products/3"
]
]
]
桃花长相依
aluckdog