joshblevins's avatar

List Pagination

I am trying to add paginate to a query that has a relationship and when I add the pagination I get error Undefined property: Illuminate\Pagination\LengthAwarePaginator::

$company = Companies::with('employees')->find($company_id)->paginate(
                    $perPage = 10,
                    Input::get('search')
            );
0 likes
6 replies
Cronix's avatar

try

$company = Companies::with('employees')->where('id', $company_id)->paginate(10);

find() executes the query. What is the purpose if Input::get('search') there?

tykus's avatar

You want to paginate the employees relation while eager-loading (with a filter)?

joshblevins's avatar

@tykus yes that is what I am trying to do...

@Cronix attempted but did not work....

I am trying to display the table with pagination as well provide a search function

tykus's avatar
tykus
Best Answer
Level 104

Don't know about the merits of eager-loading like that...

You could make two queries of your own:

$company = Company::find($id);

$employees = $company->employees()
    ->where('name', 'like', "%{$request->search}%")
    ->paginate(10);

return view('name.of.view', compact('company', 'employees'));

I don't know really how I feel about a ?page=x query string though... unless you have a route URI like: /company/{id}/employees?

joshblevins's avatar

I will try it..... Mean while I will do some research on eager loading....

joshblevins's avatar

The query worked now I just have to figure out why my search field is not working?

Please or to participate in this conversation.