ollie_123's avatar

Pagination - Call to undefined method App\Company::links()

Morning All

Im trying to use Laravels pagination rather than datatables but am a bit stuck. I keep getting the above error and cant work out why... please could one of you kind folk advise.

//Controller 
        $company = \App\Company::paginate(2);
        return view ('admin.companies', compact('company'));
//View
	@foreach ($company as $company)
		//Table
	@endforeach
{{ $company->links() }}

Thanks in advance.

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You are overriding the $company variable on every iteration of the loop, so once you come out of the loop, it is a Model instance not a LengthAwarePaginator

$companies = \App\Company::paginate(2);
return view ('admin.companies', compact('companies'));
@foreach ($companies as $company)
	//Table
@endforeach
{{ $companies->links() }}
3 likes
ollie_123's avatar

Hey @tykus

Ah that makes a lot of sense. I did try it outside of the loop before original post but then got no links but it seems pluralising & moving it outside of the loop has solved it.

Thanks for your assistance & quick response. Much appreciated.

Please or to participate in this conversation.