3

I need to send multiple object to the api resources. So I am returning multiple Object inside array.Everything works fine but not showing any pagination meta data. like Current_page,next_page url etc.

return [                  
            'across_city' => ServiceProviderCollection::collection($across_city),
            'near_by' => ServiceProviderCollection::collection($near_by)
          ];

My resource

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;

    class ServiceProviderCollection extends  Resource
    {

        public function toArray($request)
        { 

            return
            [  

                'name'=>$this->forename,
                'rating'=>$this->rating,
                'price'=>$this->price,
                'distance'=>round($this->distances,1),

            ];
        }
    }

My Controller: here I have two type of users . near_by users and Users across the city. I sent them to the ServiceProviderCollection As an array.

$user_city = $userObj->location->city;
$locationObj = Location::whereNotIn('id', $blockeduser)->where('city',$user_city)->first();
$across_city = $locationObj->users()->simplePaginate(20);

    $near_by =  User::join('locations as l', 'users.location_id', '=', 'l.id')
        ->select('users.*', DB::raw('(6371 * acos(cos(radians(' . $coordinates['latitude'] . ')) * cos(radians(`lat`)) * cos(radians(`lng`) - radians(' . $coordinates['longitude'] . ')) + sin(radians(' . $coordinates['latitude'] . ')) * sin(radians(`lat`)))) as distances'))
        ->having('distances', '<', $max_distance)
        ->orderBy('distances', 'ASC')
        ->where('role_id',2)
        ->whereNotIn('id', $blockeduser)
        ->simplePaginate(20);

    return [
                'across_city' => ServiceProviderCollection::collection($across_city),
                'near_by' => ServiceProviderCollection::collection($near_by)
           ];

I want Json Data with pagination.

{"data":{
    "across_city": [
        {
            "name": "??",
            "rating": 0,
            "price": 0,
            "distance": 0,

        }
    ],
 "links": {
        "first": "",
        "last": "",
        "prev": null,
        "next": ""
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 3,
        "path": "",
        "per_page": 2,
        "to": 2,
        "total": 6
    },
{
    "near_by": [
        {
            "name": "??",
            "rating": 0,
            "price": 0,
            "distance": 0,

        }
    ],
 "links": {
        "first": "",
        "last": "",
        "prev": null,
        "next": ""
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 3,
        "path": "",
        "per_page": 2,
        "to": 2,
        "total": 6
    }
}
7
  • what exactly are you trying to achieve? Should every collection get the pagnation-related data? Commented Sep 10, 2018 at 13:28
  • yes..every collection get pagination related data. Commented Sep 10, 2018 at 13:32
  • See answer. This should do it. Remember to accept the answer if it solvers your problem. Commented Sep 10, 2018 at 13:36
  • Whats the purpose of ServiceProviderCollection and what is in $across_city / $near_by ? Commented Sep 10, 2018 at 13:50
  • OK thanks for the details, Now its more clear. Do you get the pagination data with this: return new ServiceProviderCollection(User::paginate());? Commented Sep 10, 2018 at 14:40

1 Answer 1

2

I have no idea, what the purpose of ServiceProviderCollection is. If it creates a regular collection then there is a better way to do so.

To be able to paginate a simple collection you need to add this helper function to your base:

function paginateCollection($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (\Illuminate\Pagination\Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof \Illuminate\Support\Collection ? $items : \Illuminate\Support\Collection::make($items);
    return new \Illuminate\Pagination\LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

With this function in place you can make

return [                  
    'across_city' => paginateCollection(collect($across_city), 20),
    'near_by' => paginateCollection(collect($near_by), 20)
];

Edit

Do you have generated the corresponding resource collections?
Like so: php artisan make:resource Cities --collection

If so, you can return a paginated resource collection

Sign up to request clarification or add additional context in comments.

2 Comments

Method Illuminate\\Support\\Collection::paginate does not exist."
What do you mean with "No collections are not corresponding"? If you did not generate the resource collections then do so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.