Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active March 22, 2025 19:37
Show Gist options
  • Save atomjoy/a4ce9993f5d4a368c2d09491196cc64c to your computer and use it in GitHub Desktop.
Save atomjoy/a4ce9993f5d4a368c2d09491196cc64c to your computer and use it in GitHub Desktop.
How to paginate category model articles relations with resource class in Laravel.

Paginate Relations in Laravel

How to paginate category model articles relations with resource class in Laravel.

CategoryResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class CategoryResource extends JsonResource
{
	public function toArray(Request $request): array
	{
		return [
			'id' => $this->id,
			'name' => $this->name,
			'slug' => $this->slug,
			
			// Only for single new CategoryResource($category)
			'articles' => $this->articles()->latest('id')->paginate(6),

			// For collections with pagination in CategoryResource::collection() add custom pagination offset for relations
			// 'articles' => $this->articles()->latest('id')->offset($request->offset)->take(6))
			
			// Resource article
			'articles_2' => CategoryArticleResource::collection(
				$this->articles()->latest('id')->paginate(6)
			),
			
			// With paginator
			'articles_1' => new ArticleCollection(
				$this->articles()->latest('id')->paginate(6)
			),
		];

		// return parent::toArray($request);
	}
}

Routes

<?php

use App\Models\Category;
use App\Http\Resources\CategoryResource;
use Illuminate\Support\Facades\Route;

// Check with /test?page=2
Route::get('/test', function () {
	// For single model
	return Category::with(['articles' => function ($q) {
		$q->select('title')->where('published_at', '<', now())->paginate(2);
	}])->first();

	// For single model
	return Category::find(7)->articles()->paginate(2);

	// For single model with resource class
	return new CategoryResource(Category::find(7));

	// Works (for single model without paginate category)
	return CategoryResource::collection(Category::latest('id')->get());

	// With paginate() you need use $this->articles()->offset(request('offset'))->take(3) in resource with $request->query('offset')
	return CategoryResource::collection(Category::latest('id')->paginate(2));
});

ArticleCollection.php

Create ModelResources first for relations (ArticleResource, AuthorResource, CommentResource, ...) The collection will automatically load resources for relations.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ArticleCollection extends ResourceCollection
{
	/**
	 * Transform the resource collection into an array.
	 *
	 * @return array<int|string, mixed>
	 */
	public function toArray(Request $request): array
	{
		return [
			'data' => $this->collection,
			'pagination' => new PaginationResource($this),
		];
	}
}

PaginationResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

// Pagination resource for ModelCollections()
class PaginationResource extends JsonResource
{
	/**
	 * Transform the resource into an array.
	 *
	 * @return array<string, mixed>
	 */
	public function toArray(Request $request): array
	{
		return [
			'total' => $this->total(),
			'count' => $this->count(),
			'per_page' => $this->perPage(),
			'current_page' => $this->currentPage(),
			'total_pages' => $this->lastPage()
		];
	}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
// Pagination resource for ModelCollections()
class PaginationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'total' => $this->total(),
'count' => $this->count(),
'per_page' => $this->perPage(),
'current_page' => $this->currentPage(),
'total_pages' => $this->lastPage()
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment