neeonline's avatar

Model::paginate() and then ->append('attribute')

Hello all,

I'm using Laravel's Database Pagination (https://laravel.com/docs/5.8/pagination#basic-usage), everything work fine...

I came to a dead-[I can't find it in Google]-end. So, let's get a User model with pagination:

$users = User::paginate(15);

If needed I can add query params to the links object:

$users = User::paginate(15)->appends(['queryParam' => 'paramValue']);

But I can't append an attribute to the result set, everything that I tried failed =(

$users = User::paginate(15)->append('userAttribute');

I know it's possible to add the attribute to the protected $appends array, but this append is based on security level. So not all users should see this value.

Any help is appreciate.

Thanks!

0 likes
2 replies
Ashraam's avatar

So you want to append some data to the result array ?

$users = User::paginate(15);

$custom = collect(['my_data' => 'My custom data here']);

$data = $custom->merge($users);

Please or to participate in this conversation.