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
}