iki

laravel pagination meta data not showing

Posted 3 months ago by iki

hey guys ,

am using laravel api resources with my collection and pagination it all works fine when i return the apiresource directly but when am trying to pass the response to my custom api response function the pagination data disappear , i searched it and found that because it gets double wrapped , but i dont know how to fix it cause i need to pass it to that function , below code to explain more

resource collection

public function toArray($request)
    {
        $this->collection->transform(function (Laundry $laundry) {
            return [
                'id' => $laundry->id,
                'name' => $laundry->name,
                'description' => $laundry->description,
                'lat' => $laundry->lat,
                'lng' => $laundry->lng,
                'address' => $laundry->address,
                'has_offers' => $laundry->has_offers,
                'phone' => $laundry->phone,
                'email' => $laundry->email,
                'docs' => new LaundriesDocsResourceCollection($laundry->docs),
                'services' => new LaundriesServicesResourceCollection($laundry->services),
            ];
        });
        return $this->collection->toArray();

    }

my controller

  $laundries = Laundry::where('verified', 1)->where('expire_date','>=',Carbon::today())
            ->when($request->has_offer,function ($laundry){
                return $laundry->where('has_offers',1) ;
            })->when($request->lat && $request->lng,function ($laundry){
                // TODO:: implement filtering by lat and long logic
                return 'under implementation';
            })
            ->paginate($request->per_page ? $request->per_page : 5);
        return apiResponse(200, 'المغاسل', new LaundriesResourceCollection($laundries), true); 

my helper function

if (!function_exists('apiResponse')) {
    function apiResponse($status, $msg, $data = null, $force_data = false)
    {
        if (!$data && !$force_data) {
            return [
                'status' => $status,
                'msg' => $msg,
            ];
        } else {
            return [
                'status' => $status,
                'msg' => $msg,
                'data' => $data
            ];
        }
    }
};

Please sign in or create an account to participate in this conversation.