weaver
weaver

Reputation: 453

Paginate while returning array Api resource object

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
    }
}

Upvotes: 3

Views: 2073

Answers (1)

Gordon Freeman
Gordon Freeman

Reputation: 3421

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

Upvotes: 2

Related Questions