lundi 30 septembre 2019

Retrieving eloquent api resource using keyby collection method

I have an end API point

users/{user}

now in User resource, I want to return

     public function toArray($request)
        {
            // return parent::toArray($request);

            return [
                'id' => $this->id,
                'name' => $this->name,
//                'comments' => $this->post->comments->keyBy('post_id')
                'comments' => new CommentCollection($this->post->comments->keyBy('post_id'))

            ];
        }

but the result will not include the post_id as key, how I can make it return the comments collection having key post_id?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire