Found a solution on /r/laravel. Thanks MateusAzevedo
Letting the commentable model return its own resource:
use App\Http\Resources\VideoResource;
class Video extends Model
{
public function getResource()
{
return new VideoResource($this);
}
}
class CommentResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'body' => $this->body,
'user' => new UserResource($this->user()->first()),
'commentable' => $this->commentable()->first()->getResource(),
];
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community