Bug with API resources "with" function in Laravel 9.x

Viewed 425

I am using API Resource for returning data from the model for VueJs. However, nothing is returned differently when I apply a "with" to the resource to include the columns explicitly. I cannot get "with" to work at all, with any model, resource, anything; however, this works fine with Laravel 8.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $prescriptions = Prescription::paginate(5);

    return PrescriptionResource::collection($prescriptions);
}

Resource

class PrescriptionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'prescription' => $this->name,
            'quantity' => $this->quantity,
            'cost' => $this->cost,
            'repeat' => $this->repeat,
            'company_id' => $this->company->id,
            'company' => $this->company->name,
            'location_id' => $this->location->id,
            'location' => $this->location->name,
            'manufacturer_id' => $this->manufacturer->id,
            'manufacturer' => $this->manufacturer->name, 
            'practice_id' => $this->practice->id,
            'practice' => $this->practice->name, 
            'prescriber_id' => $this->prescriber->id,
            'prescriber' => $this->prescriber->name,                                         
        ];
    }

    /**
     * Get additional data that should be returned with the resource array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function with($request)
    {
        return [
            'meta' => [
                'columns' => [
                    'id',
                    'prescription',
                    'quantity',
                    'cost',
                    'repeat',
                    'company_id',
                    'company',
                    'location_id',
                    'location',
                    'manufacturer_id',
                    'manufacturer',
                    'practice_id',
                    'practice',
                    'prescriber_id',
                    'prescriber',
                    'created_at',
                    'updated_at',   
                ],
            ]
        ];
    }    
}

Response

{
   "data": [],
   "links": {},
   "meta": {
   "current_page": 1,
   "from": 1,
   "last_page": 181,
   "links": [],
   "path": "https://.io/project/public/prescriptions",
   "per_page": 5,
   "to": 5,
   "total": 904
}
1 Answers

You are using Resource class to add meta data. according to the docs:

Note that this does not allow any addition of custom meta data that may need to be returned with your collection. If you would like to customize the resource collection response, you may create a dedicated resource to represent the collection.

You need to create a resource collection to add meta data.

php artisan make:resource PrescriptionCollection

and then add meta data

<?php
 
namespace App\Http\Resources;
 
use Illuminate\Http\Resources\Json\ResourceCollection;
 
class PrescriptionCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'data' => $this->collection,
            'meta' => [
                'columns' => [
                    'id',
                    'prescription',
                    'quantity',
                    'cost',
                    'repeat',
                    'company_id',
                    'company',
                    'location_id',
                    'location',
                    'manufacturer_id',
                    'manufacturer',
                    'practice_id',
                    'practice',
                    'prescriber_id',
                    'prescriber',
                    'created_at',
                    'updated_at',   
                ],
        ];
    }
}

references:

https://laravel.com/docs/9.x/eloquent-resources#resource-collections

https://laravel.com/docs/9.x/eloquent-resources#adding-meta-data

Related