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);
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!
Thank you @ashraam, but to achieve the desired action (append attributes to the data result set and not the result itself), I had to use API Resources.
That way I can use the methods $when, $whenLoaded, etc.
https://laravel.com/docs/5.8/eloquent-resources#introduction
TL;DR: use API (Eloquent) Resources. As everything in Laravel, works like a charm.
Please or to participate in this conversation.