Nested relations

I have a [font="Courier New"]User[/font] model which includes [font="Courier New"]Profile[/font] which further includes [font="Courier New"]Country[/font].

I have a REST call like this:

GET api/users?expand=profile

Which returns all users with profile as expected. But I also need Country included in my profile. Something like this:

GET api/users?expand=profile,

But this does not work. Is this supported at all, or are there any other solutions oir such cases?

I’ve also tried overriding [font=“Courier New”]actionIndex[/font] to manually find all records with relations:

public function actionIndex() {

    $query = User::find()->with('profile')->all();

    return new ActiveDataProvider([

        'query' => $query,



But this does not even include [font=“Courier New”]Profile[/font]. API JSON response only shows basic users with no profile. I haven’t even tried Country. Any ideas what am I doing wrong?

Relation expansion is managed by yii\rest\Serializer. Here’s how it’s done for one model: and here’s for collection of models:

So it’s basically calls the Model::toArray method which is implemented in ArrayableTrait:

An easy option would be include the country relation in your Profile::fields.