YII2 rest api get relative data


(Sharma Kapil1989) #1

I am creating REST API in YII2. I am getting all post data by calling get API


    /post/

    /post/1/

But I want to get user also who post that particular post.

for example I want data in below format




    {

          "id":"1",

          "title":"kapil",

          "content" : "test",

          "user" : {

             "username":"admin",

             "first_name":"kapil",

             "last_name":"sharma",

              //blah blah

          }

    

    }

   

But response is


 {

              "id":"1",

              "title":"kapil",

              "content" : "test",

    }

I used this tutorial for creating API.


(Alexander Makarov) #2

(Subhapriyam) #3

http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#overriding-extra-fields

(or)

use $model->toarray([], [‘relation_name’]);