maximos's avatar

Multiple resource collection at once

Hiho :)

I have some of CollectionResources with pagination:

class PageCollectionResource extends ResourceCollection
{
	public function __construct($resource)
	{
		$this->resource = DB::table('pages')->paginate();
	}
}

class BarCollectionResource extends ResourceCollection
{
	public function __construct($resource)
	{
		$this->resource = DB::table('bars')->paginate();
	}
}

class TagCollectionResource extends ResourceCollection
{
	public function __construct($resource)
	{
		$this->resource = DB::table('tags')->paginate();
	}
}

I need to return all this resources at once:

public function IndexController extends Controller
{
	public function indexAction()
	{
		return [
			'bars' => new BarCollectionResource,
			'tags' => new TagCollectionResource,
			'pages' => new PageCollectionResource
		]
	}
}

But i got each group without pagination info:

{
  "bars": [
    {
      "name": "Name of bar"
    }
  ],
  "tags": [
    {
      "name": "Name of tag"
    }
  ],
  "pages": [
    {
      "name": "Name of page"
    }
  ]
}

I need each group with keys meta and links

0 likes
7 replies
vincent15000's avatar

I handle ressource collections like this.

Assuming you have a CourseCollection, and also a CourseResource with the method toArray().

public function toArray($request)
{
    return [
        'id' => $this->id,
        'date_fr' => $this->date_fr,
        'time_range_fr' => $this->time_range_fr,
        'hours_number' => $this->hours_number,
    ];
}

Then you can create a new course collection to retrieve a paginated courses collection.

$courses = new CourseCollection(Course::paginate($perPage));
maximos's avatar

@vincent15000 It works fine with single response:

public function IndexController extends Controller
{
	public function indexAction()
	{
		return new BarCollectionResource;
	}
}

But not in my case.

1 like
maximos's avatar

@vincent15000 Shure:

{
    "data": [
        {
            //some of key-value attributes
        },
        {
            //some of key-value attributes
        }
    ],
    "links": {
        "first": "http://site/path?p=1",
        "last": "http://site/path?p=353",
        "prev": null,
        "next": "http://site/path?p=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 353,
        "path": "http://path",
        "per_page": 10,
        "to": 10,
        "total": 3529
    }
}

So, I need this for each of group, i.e.:

{
    "bars": {
        "data": [
            {
                //some of key-value attributes
            },
            {
                //some of key-value attributes
            }
        ],
        "links": {
            "first": "http://site/bars?p=1",
            "last": "http://site/bars?p=353",
            "prev": null,
            "next": "http://site/bars?p=2"
        },
        "meta": {
            "current_page": 1,
            "from": 1,
            "last_page": 353,
            "path": "http://site/bars",
            "per_page": 10,
            "to": 10,
            "total": 3529
        }
    },
    "pages": {
        "data": [
            {
                //some of key-value attributes
            },
            {
                //some of key-value attributes
            }
        ],
        "links": {
            "first": "http://site/pages?p=1",
            "last": "http://site/pages?p=254",
            "prev": null,
            "next": "http://site/pages?p=2"
        },
        "meta": {
            "current_page": 1,
            "from": 1,
            "last_page": 254,
            "path": "http://site/pages",
            "per_page": 10,
            "to": 10,
            "total": 2312
        }
    },
    //etc
}
1 like
vincent15000's avatar

@projct1 That's strange, you should have the pagination for all groups.

Just to check, do you see the pagination datas with dd() ? And without dd(), do you have the pagination datas ?

public function IndexController extends Controller
{
	public function indexAction()
	{
		$bars = new BarCollectionResource;
		dd($bars);

		$tags = new TagCollectionResource;
		dd($tags);

		$pages = new PageCollectionResource;
		dd($pages);

		return [
			'bars' => $bars,
			'tags' => $tags,
			'pages' => $pages
		]
	}
}

Please or to participate in this conversation.