Yii2 DepDrop kartik

Ok I'm trying to use the Kartik Depdrop widget, but getting this error 500 (Internal server error)

I have a parent model Brand and submodel CarModel

Here are my files:

CarsController.php

public function actionSubcat() { $out = []; if (isset($_POST['depdrop_parents'])) { $parents = $_POST['depdrop_parents']; if ($parents != null) { $cat_id = $parents[0]; $out = self::getSubCatList($cat_id); // the getSubCatList function will query the database based on the // cat_id and return an array like below: // [ // ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'], // ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>'] // ] echo Json::encode(['output'=>$out, 'selected'=>'']); return; } } echo Json::encode(['output'=>'', 'selected'=>'']); } 

Cars.php model

 public function getSubCatList($cat_id) { $data=\common\models\CarModel::find() ->where(['brand_id'=>$cat_id]) ->select(['id','name_ru AS name' ])->asArray()->all(); return $data; } 

and my viw file

 <?php $catList = ArrayHelper::map(Brand::find()->all(),'id','name_ru'); ?> <?= $form->field($model, 'brand_id')->dropDownList($catList, [ 'prompt' => 'Select brand', 'id'=>'brand_id-id' ]); ?> <?=$form->field($model, 'car_model_id')->widget(DepDrop::classname(), [ 'options' => ['id'=>'car_model_id-id'], 'pluginOptions'=>[ 'depends'=>['brand_id-id'], 'placeholder' => 'Select...', 'url' => Url::to(['subcat']) ] ]); ?> 

What is my mistake?

+6
source share
1 answer

Code inside the controller

 $out = self::getSubCatList($cat_id); 

next change

 $out = Cars::getSubCatList($cat_id); 

and define the getSubCatList method for the static method

 public static function getSubCatList($cat_id) 
+1
source

Source: https://habr.com/ru/post/985814/


All Articles