Support the ongoing development of →
Eloquent Laravel
Last updated by @pierrejoubert73 1 year ago.

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(),
Last updated 1 year ago.
Solution selected by @pierrejoubert73

Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.