我有这个json响应。在数据部分中,我仅需要获取 uID 和电子邮件。我用JsonResource尝试过,但是它给我一个错误。
这是没有JsonResource的json响应
{
"current_page": 1,
"data": [
{
"uID": 1,
"name": "supun",
"email": "supun@gmail.com",
"email_verified_at": null,
"dob": null,
"contactNo": null,
"fbID": null,
"googleID": null,
"bloodGroup": null,
"height": null,
"weight": null,
"lID": 1,
"sID": 1,
"created_at": "2018-10-24 02:41:47",
"updated_at": "2018-10-24 02:41:47",
"deleted_at": null
},
{
"uID": 4,
"name": "supun",
"email": "supun1@gmail.com",
"email_verified_at": null,
"dob": null,
"contactNo": null,
"fbID": null,
"googleID": null,
"bloodGroup": null,
"height": null,
"weight": null,
"lID": 1,
"sID": 1,
"created_at": "2018-10-24 02:52:17",
"updated_at": "2018-10-24 02:52:17",
"deleted_at": null
}
],
"first_page_url": "http://127.0.0.1:8000/api/users?page=1",
"from": 1,
"last_page": 3,
"last_page_url": "http://127.0.0.1:8000/api/users?page=3",
"next_page_url": "http://127.0.0.1:8000/api/users?page=2",
"path": "http://127.0.0.1:8000/api/users",
"per_page": 2,
"prev_page_url": null,
"to": 2,
"total": 5
}
这是我需要使用jsonresource创建的响应
{
"current_page": 1,
"data": [
{
"uID": 1,
"email": "supun@gmail.com"
},
{
"uID": 4,
"email": "supun1@gmail.com"
}
],
"first_page_url": "http://127.0.0.1:8000/api/users?page=1",
"from": 1,
"last_page": 3,
"last_page_url": "http://127.0.0.1:8000/api/users?page=3",
"next_page_url": "http://127.0.0.1:8000/api/users?page=2",
"path": "http://127.0.0.1:8000/api/users",
"per_page": 2,
"prev_page_url": null,
"to": 2,
"total": 5
}
这是我的用户 UserController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Resources\Users as GetAllUsersResource;
use Validator;
class UserController extends Controller {
public function usersApi( Request $request ) {
$userInfo = User::paginate(2);
$output = new GetAllUsersResource($userInfo);
return response()->json($output, $this->successStatus);
// return response()->json(['status' => true,
// 'message' => 'done',
// 'data' => $output
// ], $this->successStatus);
}
}
这是我的 JsonResource
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Users extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
//return parent::toArray($request->data);
return [
'uID' =>$this->uID,
'email' =>$this->email,
];
}
}
这是将其添加到JsonResource后得到的错误
"message": "Undefined property: Illuminate\\Pagination\\LengthAwarePaginator::$uID",
"exception": "ErrorException",
"file": "E:\\xampp\\htdocs\\myworks\\pharmeasylk\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Resources\\DelegatesToResource.php",
"line": 120,
我尝试了许多其他方法,但是没有运气。仅当用户仅返回一行时,此功能才有效。但是分页包含许多用户数据和链接数据。如果有人可以帮助我。很大的帮助。
答案 0 :(得分:0)
__construct()
类中的GetAllUsersResource
方法做什么
$output = new GetAllUsersResource($userInfo);
您可能想这样做!!
$output = new GetAllUsersResource($userInfo->items());
答案 1 :(得分:0)
似乎您正在将分页的集合传递给Illuminate\Http\Resources\Json\JsonResource
类(期望有一个对象),而不是Illuminate\Http\Resources\Json\ResourceCollection
类,后者期望接收一个集合。
来自docs:
除了生成可转换单个模型的资源外, 您可能会产生负责转型的资源 模型集合。这样,您的回复即可包含链接和 与整个集合相关的其他元信息 给定资源。
要创建资源集合,应使用--collection标志 创建资源时。或者,在“ 资源名称将向Laravel指示应创建一个 收集资源。集合资源扩展了 Illuminate \ Http \ Resources \ Json \ ResourceCollection类:
php artisan make:resource Users --collection
php artisan make:resource UserCollection
如果您不打算花任何时间来转换您的集合,这也可能在不更改JsonResource类的情况下起作用:
$output = GetAllUsersResource::collection($userInfo);