Support the ongoing development of Laravel.io →
Eloquent Laravel
Last updated by @pierrejoubert73 2 weeks ago.
0

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 2 weeks ago.
0
Solution selected by @pierrejoubert73

Sign in to participate in this thread!

LoadForge

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2023 Laravel.io - All rights reserved.