boyjarv's avatar

Now I'm using relationships and collections my pagination and meta data has gone and I'm unable to paginate

Here is the method that should paginate my contacts... but since I have been messing about with relationships, pagination has stopped working and I'm unable to see data.meta in my JSON output in Postman?!

public function index()
    {
        $contacts = Contact::paginate(15);

        return ContactResource::collection($contacts->load('company'));
    }
0 likes
6 replies
tykus's avatar

You can eagerload directly:

$contacts = Contact::with(‘company’)->paginate(15);

My experience is that the collection method should accept a Collection of Contacts

boyjarv's avatar

@tykus doing it this way:

public function index()
    {
        $contacts = Contact::with('company')->paginate(15);

        return ContactResource::collection($contacts->load('company'));
    }

I'm getting: Undefined method 'load'.intelephense(1013)

tykus's avatar
tykus
Best Answer
Level 104

@boyjarv the company relation is already loaded whenever you eager-load with('company')

public function index()
{
    $contacts = Contact::with('company')->paginate(15);

    return ContactResource::collection($contacts);
}
1 like
boyjarv's avatar

@Tray2 ok so I tried this:

public function index()
    {
        $contacts = Contact::query()
        ->join('companies','contacts.company_id', '=', 'companies.id')->paginate(50);

        return ContactResource::collection($contacts->load('company'));
    }

I'm still not getting data.meta

Tray2's avatar

@boyjarv Remove this return ContactResource::collection($contacts->load('company')); , and return contacts instead.

Please or to participate in this conversation.