I have this json response. In data section i need to get only uID and email only. I try it with JsonResource but it give me a error.
This is json respond without JsonResource
{
"current_page": 1,
"data": [
{
"uID": 1,
"name": "supun",
"email": "[email protected]",
"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": "[email protected]",
"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
}
This is the respond i need to create using jsonresource
{
"current_page": 1,
"data": [
{
"uID": 1,
"email": "[email protected]"
},
{
"uID": 4,
"email": "[email protected]"
}
],
"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
}
This is my user 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);
}
}
and this is my 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,
];
}
}
This is the error i got after add it to 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,
I try many other ways but no luck. this work only for if user return only one row. but pagination contain many user data and links data . If some can help me with it. big help.