@Sabonzy Resource classes are for creating JSON representations of models; that’s why they extend a class called JsonResource. They’re not meant to be used to “transform” a model.
API Resource and passing data to blade view
i am using the api resource to to transform the models before returning it a vue component through blade view but the data returned is not the transformed model but the raw model itself. Interestingly when the data is returned directly without passing it to the view then it will be formatted as described in the toArray method.
here is the controller:
class PreApprovedController extends Controller
{
private $applicants;
public function __construct(PersonalDetailsRepository $applicants)
{
$this->applicants = $applicants;
}
public function show($id)
{
$applicant = $this->applicants->withCriteria(
new WithRelations(
[
'batteries',
'inverters',
'solarPanels',
'bosInfo',
'location',
]
))->find($id);
// dd(new InspectionsResource($applicant)); this returns the raw model model not the transformed one
// return new InspectionsResource($applicant); this returns the the transformed model
$applicant = new InspectionsResource($applicant));
return view('approved.inspections.create', compact('applicant'));
}
}
the api resource:
class InspectionsResource extends JsonResource
{
public function toArray($request)
{
return [
"id" => $this->id,
"full_name" => $this->full_name,
"phone_number" => $this->phone_number,
"identification_type" => $this->identification_type,
"identification_number" => $this->identification_number,
"postal_address" => $this->postal_address,
"email" => $this->email,
"profession" => $this->profession,
"company_name" => $this->company_name,
"company_location" => $this->company_location,
"location" => new LocationsResource($this->whenLoaded('location')),
"batteries" => new BatteriesResource($this->whenLoaded('batteries')),
"inverters" => new InvertersResource($this->whenLoaded('inverters')),
"solar_panels" => new SolarPanelsResource($this->whenLoaded('solarPanels')),
"bos_info" => new BoSResource($this->whenLoaded('bosInfo')),
];
}
}
formatted data is also return when i convert it collections. how do i get the transformed data returned when getting a single model
@Sabonzy That’s because the toArray() method is only called when the JsonResource instance is actually used as a response (and not as a variable passed to a view).
Calling properties and methods on a resource class just delegates to the underlying Eloquent model via the DelegatesToResource trait so like I say, a resource class doesn’t “transform” the value as you’d expect.
If you want the “JSON” resource version of a model, you can call the toArray() method on the resource class directly:
$model = new ModelResource($model);
return view('model.show', [
'model' => $model->toArray(),
]);
However, this is using JSON resources for a purpose they weren’t intended for.
If you are just wanting to transform models, then you might consider using Fractal transformers.
Please or to participate in this conversation.