To paginate both the DeviceType and its related devices, you need to handle pagination separately for each. The paginate method in Eloquent only applies to the main query, not the related models. Here's how you can achieve pagination for both:
-
Paginate the
DeviceTypemodels: Use thepaginatemethod on theDeviceTypemodel. -
Paginate the
devicesfor eachDeviceType: Use thepaginatemethod on thedevicesrelationship within a loop.
Here's an example of how you can implement this:
use App\Models\DeviceType;
$deviceTypes = DeviceType::paginate(10);
foreach ($deviceTypes as $deviceType) {
// Paginate the devices for each DeviceType
$deviceType->devices = $deviceType->devices()->paginate(5);
}
// Now you can pass $deviceTypes to your view
In this example, DeviceType::paginate(10) paginates the DeviceType models, and for each DeviceType, we paginate the devices with ->paginate(5). Adjust the pagination numbers as needed for your application.
In your view, you can then iterate over $deviceTypes and for each DeviceType, iterate over the paginated devices. This way, both the DeviceType and its related devices are paginated.