Yii Framework Forum: YII2 rest api get relative data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

YII2 rest api get relative data

#1 User is offline   kapilsharma 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-April 17

Posted 11 April 2017 - 05:57 AM

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][1] tutorial for creating API.


[1]: http://www.yiiframew...uick-start.html
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 11 April 2017 - 08:42 AM

https://github.com/s...3edf63ad3ba3b9d
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   subha 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 09-October 14

Posted 18 April 2017 - 04:21 AM

http://www.yiiframew...ng-extra-fields
(or)
use $model->toarray([], ['relation_name']);
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users