How to extend Eloquent Collection for use with mezzio-hal?

26 views Asked by At

I have a Collection with paginator, _links and _embedded keys, which response using it and mezzio-hal looks like this:

{
  "_total_items": 20,
  "_links": {
    "self": {
      "href": "http://localhost/api/v3/responses?with[0]=users&with[1]=vacancies"
    }
  },
  "_embedded": {
    "responses": [
      {
        "id": 1,
        "vacancy_id": 5,
        "user_id": 11
      },
      {
        "id": 2,
        "vacancy_id": 6,
        "user_id": 5
      }
    ]
  }
}

Now I want the response to have this structure:

{
  "_total_items": 20,
  "_links": {
    "self": {
      "href": "http://localhost/api/v3/responses?with[0]=users&with[1]=vacancies"
    }
  },
  "_embedded": {
    "responses": [
      {
        "id": 1,
        "vacancy_id": 5,
        "user_id": 11
      },
      {
        "id": 2,
        "vacancy_id": 6,
        "user_id": 5
      }
    ],
    "vacancies": [
      {
        "id": 5,
        "name": "Vacancy 5",
        "status": "opened"
      },
      {
        "id": 6,
        "name": "Vacancy 6",
        "status": "closed"
      }
    ],
    "users": [
      {
        "id": 11,
        "username": "joe"
      },
      {
        "id": 5,
        "username": "frank"
      }
    ]
  }
}

So I can have two extra keys in _embedded, keeping pagination of initial collection. How do I do this with Eloquent Collection?

PS: For mezzio-hal I'm using RouteBasedCollectionStrategy and RouteBasedResourceStrategy. I tried building a collection from scratch which has given me error of mezzio-hal. I tried adding properties to existing collection, but no keys were added to response.

0

There are 0 answers