1

I want to different type of collection..One containing users acros the city and another one containing near by users. I want these from single api hit. is it possible ? If yes then please suggest how to do that.

Waht I did

return  ServiceProviderCollection::collection($near_by);

Output:

"data": [
    {
        "username": "??",
        "email": "??",
        "rating": 0,
        "role_id": 2,
        "wallet": "0"
    }
],

I want

return  ServiceProviderCollection::collection($near_by,$across_city);

expected output:

{
    "across_city": {
        "data": [
            {
                "username": "??",
                "email": "??",
            }
        ],
    },
    "near_by": {
        "data": [
            {
                "username": "??",
                "email": "??",

            }
        ],

    }
}
2
  • What exactly is the class ServiceProviderCollection? This does not seem to be part of Laravel itself. Commented Sep 10, 2018 at 9:33
  • Laravel's resources collection. Collection resources extend the Illuminate\Http\Resources\Json\ResourceCollection class. More ditails: laravel.com/docs/5.7/eloquent-resources Commented Sep 10, 2018 at 9:37

2 Answers 2

3

No, you can't pass 2 objects in Resource. You can do it like this

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

Edit: After comment

If you want to show pagination information then you have to create separate controller action and then return ServiceProviderCollection::collection then you will get result with pagination meta information.

create these action in your controller ex. (UserController)

public function acrossCity(){
   $acrossCity = User::where('city','test')->paginate(10); //example 

   return ServiceProviderCollection::collection($acrossCity);
}

public function nearBy(){
   $nearBy = User::where('near','1')->paginate(10); //example 

   return ServiceProviderCollection::collection($nearBy);
}

create routes for these

Route::get('user/acrossCity','UserController@acrossCity');
Route::get('user/nearBy','UserController@nearBy');

Check document https://laravel.com/docs/5.6/eloquent-resources#pagination

Note: when using resource class then name it without Collection. For your case you should name resource as ServiceProviderResource and then when you call its collection then ServiceProviderResource::collection($object) but when returning single object then new ServiceProviderResource($object).

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

6 Comments

ServiceProviderCollection::collection($across_city)//paginate working. But when i am using your above code paginate working but not showing any meta value like current page,next page link etc.
@weaver If you want to show pagination information then you have to create separate controller action and then return ServiceProviderCollection::collection then you will get result with pagination meta information
@rkj..Thanks..I am trying to do so..Buti have zero idea how to do that.would you please explain in details?
You used two different routes. mean i can't get both result near_by and across_city at same time. i need both result on single route hit
|
3

I am currently using Laravel 7 and in my controller I am passing an array of collection objects to resource class

 $data = ['quotation' => Quotation::first()]; 
 return new QuotationResource($data);

and in my resource class I can access the data using

   public function toArray($request)
   {
        return [
            'quotation' => $this->resource['quotation']
        ];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.