1

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.

2 Answers 2

1

It looks like you're giving the collection to an Illuminate\Http\Resources\Json\JsonResource class (which expects to receive a single object) instead of giving it to a Illuminate\Http\Resources\Json\ResourceCollection class, which expects to receive a collection.

From the docs:

In addition to generating resources that transform individual models, you may generate resources that are responsible for transforming collections of models. This allows your response to include links and other meta information that is relevant to an entire collection of a given resource.

To create a resource collection, you should use the --collection flag when creating the resource. Or, including the word Collection in the resource name will indicate to Laravel that it should create a collection resource. Collection resources extend the Illuminate\Http\Resources\Json\ResourceCollection class:

php artisan make:resource Users --collection

php artisan make:resource UserCollection

https://laravel.com/docs/5.7/eloquent-resources#generating-resources

If you're not doing anything fancy to transform your collection, this might also work without changing your JsonResource class:

$output = GetAllUsersResource::collection($userInfo);
Sign up to request clarification or add additional context in comments.

Comments

0

what does the __construct() method do in the GetAllUsersResource class

$output = new  GetAllUsersResource($userInfo);

you might wanted to do this instead !?

  $output = new  GetAllUsersResource($userInfo->items());

4 Comments

Good Sir I done as you mention but i get this error Cannot access protected property Illuminate\\Pagination\\LengthAwarePaginator::$items . I'm new to Laravel .can you bit explain to me.
sorry, access it as a method items(). it will return an array with the users in it
But Sir i need other data also like (last_page, per_page , total) after add $output = new GetAllUsersResource($userInfo->items()); it give me this error Trying to get property 'uID' of non-object
post in your question the class GetAllUserResource

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.