Reputation: 62394
I'm trying to use Laravel's API Resources to handle some JSON and I'm not able to conditionally load a relationship, despite it being eager loaded.
My controller:
$games = Game::with('availableInterests')->get();
In my view, I'm json encoding the collection to be used in VueJS
games = @json(new \App\Http\Resources\GameCollection($games)),
GameCollection - unchanged from the class Laravel generated for me.
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class GameCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
GameResource
class GameResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'thumbnail_url' => $this->thumbnail_url,
'available_interests' => Interest::collection($this->whenLoaded('availableInterests')),
];
}
}
Game Model's Relationship
public function availableInterests() : BelongsToMany
{
return $this->belongsToMany(Interest::class);
}
I've tried changing $this->whenLoaded('availableInterests')
to $this->whenLoaded('available_interests')
with no luck. I've double checked my spelling with no luck.
Why isn't this conditional relationship present in the json?
Even removing $this->whenLoaded()
doesn't make this relationship appear.
Upvotes: 2
Views: 2103
Reputation: 3186
I don't think you need GameCollection in this case.
I would try doing this:
InterestResource.php (create new class)
class InterestResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
// or what ever array structure you want
}
}
GameResource.php
class GameResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'thumbnail_url' => $this->thumbnail_url,
'available_interests' => InterestResource::collection($this->whenLoaded('availableInterests')),
];
}
}
Your Controller
$games = Game::with('availableInterests')->get();
Your View
games = @json(\App\Http\Resources\GameResource::collection($games)),
Upvotes: 1