mastermindlegion's avatar

Laravel pagination and resources problem

Hello guys, I have problem regarding to the pagination, where resuls are only displayed for the ?page=1 and if I try different page, only empty [] is returned.

I guess I have to paginate AdvertisementResource

Api Route:

Route::get('/categories/{advertisementCategory:slug}', 'Advertisements\AdvertisementCategoryController@indexOneCategory');

Controller:

 public function indexOneCategory(AdvertisementCategory $advertisementCategory)
    {
            $categoryID = $advertisementCategory->id;
            return AdvertisementCategoryResource::collection(AdvertisementCategory::where('id', $categoryID )->paginate(1));
    }

AdvertisementCategoryResouce:

 public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'slug' => $this->slug,
            'advertisements' => AdvertisementResource::collection($this->advertisements),
        ];
    }

AdvertisementResource

    public function toArray($request)
    {
        return[
            'id' => $this->id,
            'category' => $this->categoryParent(),
            'advertisement_category_id' => $this->advertisement_category_id,
            'user_id' => $this->user_id,
            'name' => $this->name,
            'slug' => $this->slug,
            'location' => $this->location,
            'body' => $this->body,
            'views' => $this->views,
            'order' => $this->order,
            'user' => new UserAdvertisementResource($this->user),


        ];
    }

Result?page=2

{
  "data": [],
  "links": {
    "first": "http:\/\/www.pravopress.test:8080\/api\/categories\/test?page=1",
    "last": "http:\/\/www.pravopress.test:8080\/api\/categories\/test?page=1",
    "prev": "http:\/\/www.pravopress.test:8080\/api\/categories\/test?page=1",
    "next": null
  },
  "meta": {
    "current_page": 2,
    "from": null,
    "last_page": 1,
    "path": "http:\/\/www.pravopress.test:8080\/api\/categories\/test",
    "per_page": 1,
    "to": null,
    "total": 1
  }
}

Result ?page=1

{
  "data": [
    {
      "id": 21,
      "name": "test",
      "slug": "test",
      "advertisements": [
        {
          "id": 1,
          "category": "test",
          "advertisement_category_id": 21,
          "user_id": 1,
          "name": "Test advertisement",
          "slug": "test-advertisement",
          "location": "Prague",
          "body": "lorem ipsum ",
          "views": 1,
          "order": 1,
          "user": {
            "id": 1,
            "full_name": "Martin Chalupa",
            "full_name_and_degree": "JUDr., PhDr. Martin Chalupa LLM.",
            "email": "[email protected]",
            "role": "empty",
            "avatar": "http:\/\/www.pravopress.test:8080\/storage\/avatars\/default.png",
            "profile": {
              "id": 1,
              "user_id": 1,
              "description": "If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:",
              "working_on": "Poskytování nejlepších služeb",
              "learning": "Svěřenské fondx' => 'Překročena kapthis.submitSuccess = truethis.submitSuccess = truethis.submitSuccess = truethis.suthis.submitSuccess = truethis.submitSuccess = truethis.submitSuccess = truethis.submitSuccess = truebmitSuccess = truethis.submitSuccess = truethis.submitSuccess = true",
              "interests": "Právo, sport, programming",
              "location": "Praha this.handleErrors(e) this.handleErrors(e) this.handleErrors(e) this.handleErrors(e)",
              "advocate_office": "JUDr. Luboš Chalupa",
              "job_search": 1,
              "teaching": 0,
              "front_degree": "JUDr., PhDr.",
              "back_degree": "LLM.",
              "facebook": null,
              "twiter": null,
              "website": null,
              "email": "[email protected]",
              "show_email": 1,
              "created_at": "2020-09-02T14:49:35.000000Z",
              "updated_at": "2020-09-07T07:40:11.000000Z"
            },
            "created_at_diff": "před 2 týdny",
            "updated_at": "2020-09-02T16:39:22.000000Z"
          }
        },
        {
          "id": 3,
          "category": "test",
          "advertisement_category_id": 21,
          "user_id": 2,
          "name": "Juuu",
          "slug": "juuu",
          "location": null,
          "body": null,
          "views": 1,
          "order": 1,
          "user": {
            "id": 2,
            "full_name": "New user New",
            "full_name_and_degree": " New user New ",
            "email": "[email protected]",
            "role": "empty",
            "avatar": "http:\/\/www.pravopress.test:8080\/storage\/avatars\/default.png",
            "profile": {
              "id": 2,
              "user_id": 2,
              "description": null,
              "working_on": null,
              "learning": null,
              "interests": null,
              "location": null,
              "advocate_office": null,
              "job_search": 0,
              "teaching": 0,
              "front_degree": null,
              "back_degree": null,
              "facebook": null,
              "twiter": null,
              "website": null,
              "email": null,
              "show_email": 0,
              "created_at": "2020-09-04T13:31:29.000000Z",
              "updated_at": "2020-09-04T13:31:29.000000Z"
            },
            "created_at_diff": "před 2 týdny",
            "updated_at": "2020-09-04T13:31:28.000000Z"
          }
        },
        {
          "id": 6,
          "category": "test",
          "advertisement_category_id": 21,
          "user_id": 2,
          "name": "Listing new",
          "slug": "listing-new",
          "location": null,
          "body": null,
          "views": 1,
          "order": 1,
          "user": {
            "id": 2,
            "full_name": "New user New",
            "full_name_and_degree": " New user New ",
            "email": "[email protected]",
            "role": "empty",
            "avatar": "http:\/\/www.pravopress.test:8080\/storage\/avatars\/default.png",
            "profile": {
              "id": 2,
              "user_id": 2,
              "description": null,
              "working_on": null,
              "learning": null,
              "interests": null,
              "location": null,
              "advocate_office": null,
              "job_search": 0,
              "teaching": 0,
              "front_degree": null,
              "back_degree": null,
              "facebook": null,
              "twiter": null,
              "website": null,
              "email": null,
              "show_email": 0,
              "created_at": "2020-09-04T13:31:29.000000Z",
              "updated_at": "2020-09-04T13:31:29.000000Z"
            },
            "created_at_diff": "před 2 týdny",
            "updated_at": "2020-09-04T13:31:28.000000Z"
          }
        }
      ]
    }
  ],
  "links": {
    "first": "http:\/\/www.pravopress.test:8080\/api\/categories\/test?page=1",
    "last": "http:\/\/www.pravopress.test:8080\/api\/categories\/test?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 1,
    "path": "http:\/\/www.pravopress.test:8080\/api\/categories\/test",
    "per_page": 1,
    "to": 1,
    "total": 1
  }
}
0 likes
2 replies
Graveman's avatar

For anyone who's struggling, just use axios.all:

axios.all([axios.get(api/advertisement_category?page=${label}), axios.get('api/advertisement')]) .then(axios.spread((res1, res2)=>{ console.log(res1.data) console.log(res2.data) }))

Please or to participate in this conversation.