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?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ozTvtH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire