Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen.talari's avatar

How to Paginate Eloquent hasMany relation?

i have 'user' and 'sites' table with hasMany relation

User Model looks as

public function sites() {
        return $this->hasMany(Sites::class, 'user_id', 'id');
    }

Sites Model looks as

public function user() {
        return $this->belongsTo(User::class,'user_id','id');
    }

In my controller, i tried to get all sites of a user and it works fine.

User::find(122)->sites

Now, how can i make this paginate ?

User::find(122)->sites->paginate() is failing.

0 likes
14 replies
pmall's avatar
pmall
Best Answer
Level 56
User::find(122)->sites()->paginate();

User::find(122)->sites->paginate(); doesnt work because you are trying to call the paginate method on the sites collection. With the parenthesis you call it on the return value of the relationship method, which is the relationship object, which can be used as a query builder.

36 likes
d3adr1nger's avatar

@pmall Just to give another use case for future reference:

An example of paginating supported airports of an air transport. The Relationship is manytomany.

$airports = $airTransport->airports();
return view('yourView', [
   'airports' => $airports->paginate(10),
]);

This example also works perfectly with Livewire. Hope this helps 😁

4 likes
Donika's avatar

Can i use smth like this with all categories?

 public function index(Request $request){

        $categories= Category::all()->posts()->paginate(1);
        return response()->json($categories);
    }

And in response = data[{title: "some title", posts:['there is posts array']}]

1 like
fakhridarmawan's avatar

$categories= Category::all()->posts()->paginate(1); doesn't work, if you want to use all() you can query using with method like this: $categories= Category::with('posts')->paginate(1);

3 likes
hanif-king's avatar

Hi i solved this problem Please follow this line of my code. you need to specify on which parent row you want to display related data from it's child table so in this case i am specifying first row of parent as my row and then i am paginating child records according to that specific parent row.

$dataPaginater=user::where('user_id',$someID)->first()->sites()->paginate(10);

this code works in laravel 5.4 thanks

sineld's avatar

You may not use relation actually in this situation:

    $category = Category::query()
        ->where('slug', $slug)
        ->first();

    $products = Product::query()
        ->where('category_id', $category->id)
        ->paginate(4);
2 likes
zymawy's avatar
$category = Category::where('slug', $slug)->first();

$category->setRelation('lessons', $category->lesson()->paginate(10));

16 likes
Nasr1's avatar

@zymawy thanks brothers , this is the golden solution , god bless u :)

Cheikh's avatar

Thanks Bro!

This solved the problem on my Laravel 7's app.

johndivam's avatar
#in repository      
 $actions = $user->actions()->paginate(50);
 

#in blade
{{ $actions->links('pagination.default') }}
2 likes

Please or to participate in this conversation.