Vit
Vit

Reputation: 154

How to show pagination from controller Laravel

The question is not directly on how to show the pagination that much I'm aware but rather how to modify it so that I can show it here

public function create()
    {
        $leaves = Leave::latest()->where('user_id',auth()->user()->id)->get();
        $pagination = Leave::latest()->paginate(5);
        return view('leave.create',compact('leaves'),compact('pagination'));
    }

The create function here returns to a page where you can create and view all that you have created at the same time on a table but the table keeps getting bigger so I wanted to add pagination here but it seems this is not the correct way I tried adding the ->paginate(5) right after latest() of $leaves but it showed an error

Is there another way I can add the pagination, the one right now works but it shows the leaves of all users but I need it to only show of that specific user that's why I added the auth up but I think the one under for pagination is overriding it

Upvotes: 3

Views: 340

Answers (2)

Abdo
Abdo

Reputation: 36

$leaves = Leave::latest('users')->limit(5)

you can specify the number of rows you want to get limit(number)

Upvotes: 1

Aless55
Aless55

Reputation: 2709

Instead of using the get() method at the end of your leaves query you can simply call paginate(5).

$leaves = Leave::latest()->where('user_id',auth()->user()->id)->paginate(5);

Upvotes: 3

Related Questions