why dont display relation data in Rest Yii2
i have two tables.
sample: category , subcategory
<?php
namespace app\controllers;
use app\models\Category;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\rest\Controller;
class ApiController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats'] = ['application/json' => Response::FORMAT_JSON];
return $behaviors;
}
public function actionGetSk($cId)
{
$result= Category::find()->with('subCategory')->where(['id' => $cId])->all()
return $result;
}
}
i result i have only from Category. (result is json)
but print_r($result) i have data from Category and subCategory.
web.php
[
'class' => 'yii\rest\UrlRule',
'pluralize' => false,
'controller' => 'api',
],
Try this: in your
Category
model, add this method:And now, call your api with the
expand
get parameter like: