Support the ongoing development of Laravel.io →
Eloquent Laravel
Last updated by @pierrejoubert73 1 year 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 1 year ago.
0
Solution selected by @pierrejoubert73

Sign in to participate in this thread!

Eventy

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.

© 2024 Laravel.io - All rights reserved.