demonz's avatar

can I send meta data without using eloquent resources ?

Hello , I am trying to make pagination for the table , but I don't know how to pass meta data because I didn't use eloquent api resources

   public function index()
    {
        $users=User::has('learner')
        ->with('learner')
        ->orderBy('id')
        ->filter(request()->only('search'))
        ->paginate()
        ->withQueryString();

        return Inertia::render('Learner/Index', [
            'filters' => request()->all('search'),
            'users' => $users->map(function ($user) {
                return [
                    'id' => $user->id,
                    'reference_id' => $user->reference_id,
                    'email' => $user->email,
                    'name' => $user->learner->first_name.' '.$user->learner->second_name.' '.$user->learner->last_name ,
                    'mobile' => $user->learner->mobile,
                    'status' => $user->learner->status,
                ];
            }),
        ]);
    }

I want to implement what this guy did https://github.com/Landish/pingcrm-react/blob/main/resources/js/Shared/Pagination.js

I can't get the label,active,url , do I have to make resources ?

0 likes
3 replies
LaryAI's avatar
Level 58

Yes, you can send meta data without using eloquent resources. You can use the links() method on the paginator instance to get the meta data. For example:

$users=User::has('learner')
    ->with('learner')
    ->orderBy('id')
    ->filter(request()->only('search'))
    ->paginate()
    ->withQueryString();

$meta = $users->links();

return Inertia::render('Learner/Index', [
    'filters' => request()->all('search'),
    'users' => $users->map(function ($user) {
        return [
            'id' => $user->id,
            'reference_id' => $user->reference_id,
            'email' => $user->email,
            'name' => $user->learner->first_name.' '.$user->learner->second_name.' '.$user->learner->last_name ,
            'mobile' => $user->learner->mobile,
            'status' => $user->learner->status,
        ];
    }),
    'meta' => $meta
]);

The links() method will return an array of meta data, including the labels, active page, and URLs for the pagination links.

demonz's avatar

@LaryAI how do I access the labels, active page, and URLs from the meta variable in the frontend React

David69's avatar

The main difference between a JSON resource and a JSON collection is that a resource extends the JsonResource class and expects a single resource to be passed when being instantiated while a collection extends the ResourceCollection class and expects a collection as the argument when being instantiated.

Please or to participate in this conversation.