Empty result in second level of kartik depdrop in Yii2
<p>Here am trying Dependent dropdown using kartik depdrop yii2 extension . the process of this dependent dropdown is, if i select a productname it will show me the dependent batchno, then if select a batchno, it will show the dependent itemid.</p>

<p>Actually first level is working perfectly, if i select a productname, it will show me batchno, this action is working perfectly, but the problem on the second level. if i select a batchno it need to show me a itemid, this action is not working</p>

<p>And am getting error as this image - <a href="https://i.stack.imgur.com/UeX2J.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UeX2J.jpg" alt="Empty Result"></a></p>

<p>Controller</p>

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

    public function actionProd() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $ids = $_POST['depdrop_parents'];
        $cat_id = empty($ids[0]) ? null : $ids[0];
        $subcat_id = empty($ids[1]) ? null : $ids[1];
        if ($cat_id != null) {
           $data = Productbatch::getItemid($cat_id, $subcat_id);
            /**
             * the getProdList function will query the database based on the
             * cat_id and sub_cat_id and return an array like below:
             *  [
             *      'out'=&gt;[
             *          ['id'=&gt;'&lt;prod-id-1&gt;', 'name'=&gt;'&lt;prod-name1&gt;'],
             *          ['id'=&gt;'&lt;prod_id_2&gt;', 'name'=&gt;'&lt;prod-name2&gt;']
             *       ],
             *       'selected'=&gt;'&lt;prod-id-1&gt;'
             *  ]
             */
           echo Json::encode($out);
           //echo Json::encode(['output'=&gt;$out, 'selected'=&gt;$data['selected']]);
           return;
        }
    }
    echo Json::encode(['output'=&gt;'', 'selected'=&gt;'']);
}
</code></pre>

<p>_form</p>

<pre><code>&lt;?= $form-&gt;field($model, 'productname')-&gt;widget(Select2::classname(), [
    'data' =&gt; ArrayHelper::map(Productnames::find()-&gt;all(),'productnames_productname','productnames_productname'),
    'language' =&gt; 'en',
    'options' =&gt; ['placeholder' =&gt; 'Select Product Name', 'id' =&gt; 'cat-id'],
    'pluginOptions' =&gt; [
        'allowClear' =&gt; true
    ],
    ]); ?&gt;

    &lt;?= $form-&gt;field($model, 'batchno')-&gt;widget(DepDrop::classname(), [
    'options'=&gt;['id'=&gt;'subcat-id'],
    'pluginOptions'=&gt;[
        'depends'=&gt;['cat-id'],
        'placeholder'=&gt;'Select BatchNo',
        'url'=&gt;Url::to(['/production/productbatch/subcat'])
    ]
    ]); ?&gt;

    &lt;?= $form-&gt;field($model, 'itemid')-&gt;widget(DepDrop::classname(), [
    'pluginOptions'=&gt;[
        'depends'=&gt;['cat-id', 'subcat-id'],
        'placeholder'=&gt;'Select ItemId',
        'url'=&gt;Url::to(['/production/productbatch/prod'])
    ]
    ]); ?&gt;
</code></pre>

<p>Model</p>

<pre><code>public static function getBatchNo($cat_id)
    {
        $out = [];
        $data = Productbatch::find()
                -&gt;where(['productname' =&gt; $cat_id])
                -&gt;asArray()
                -&gt;all();
        foreach ($data as $dat) {
            $out[] = ['id' =&gt; $dat['itemid'], 'name' =&gt; $dat['batchno']];
        }
        return $output = [
            'output' =&gt; $out,
            'selected' =&gt; ''
        ];
    }

    public static function getItemid($cat_id, $subcat_id)
    {
        $out = [];
        $data = Productbatch::find()
                -&gt;where(['productname' =&gt; $cat_id])
                -&gt;andWhere(['batchno' =&gt; $subcat_id])
                -&gt;asArray()
                -&gt;all();

        $selected = '';

        foreach ($data as $dat =&gt; $datas) {
            $out[] = ['id' =&gt; $datas['itemid'], 'name' =&gt; $datas['itemid']];

            if($dat == 0){
                    $aux = $datas['itemid'];
                }

            ($datas['productname'] == $cat_id) ? $selected = $cat_id : $selected = $aux;

        }
        return $output = [
            'output' =&gt; $out,
            'selected' =&gt; $selected
        ];
    }
</code></pre>