0
votes

I want to limit my applicants list using pagination but i cant find a way to work it with auth(). is there a way for this to work?

Using the $users = User::paginate(5); works but i want to use the auth() for security reasons

already tried

'applicants' => auth()->user()->applicants->paginate(20)
'applicants' => auth()->user()->paginate(20)->applicants

User.php

// Model
public function applicants()
    {
       return $this->hasMany(Scholar::class,'owner_id');
    }

ApplicantController.php

public function index()
    {

        // show all applicants
        return view('applicants/index', [
            'applicants' => auth()->user()->applicants //reutrn as collection if i dd()
        ]);
    }

foo.blade.php

{{ $applicants->links() }}
{{ $applicants->onEachSide(5)->links() }}

routes/web.php

Route::resource('applicants', 'ApplicantController');
1

1 Answers

3
votes

Pagination works on a Query Builder or Eloquent instance. You are attempting to paginate a Collection.
https://laravel.com/docs/6.x/pagination#basic-usage

Without parentheses, this line returns a collection.
At this point, the query has been sent and the database has returned the results.

auth()->user()->applicants

With parentheses, this line returns a query builder instance.
At this point, the the query has not been sent to the database.

auth()->user()->applicants()

Try updating your code to account for this small but important distinction.

'applicants' => auth()->user()->applicants()->paginate(20)

// or some may prefer to explicitly wrap the user model
'applicants' => (auth()->user())->applicants()->paginate(20)