ft ab
ft ab

Reputation: 9

how to generating a dropdown list in yii2

I tried to make a dropdown list in yii2 using this link : How to make a drop down list in yii2?

my code is :

<?php use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
<?= $form->field($model, 'parent_id')-> dropDownList($listdata);  ?>

but I have a problem in line of using ArrayHelper
the problem is: PHP Notice – yii\base\ErrorException Array to string conversion.......! I tested the below code :

 $listData=ArrayHelper::map(Product::find()->asArray()->all(),'id','name');     

but it dos not solved and has the same error!

whats the problem? can somebody help me?

Upvotes: 0

Views: 1269

Answers (2)

Ammu
Ammu

Reputation: 55

Try like this

   <?php
     use yii\helpers\ArrayHelper;
     use app\models\Product;
    ?>

   <?= $form->field($model, 'parent_id')->dropDownList(
        ArrayHelper::map(Product::find()->all(),'id','name'),
        ['prompt'=>'Select ']) 
    ?>

Upvotes: 0

Imre L
Imre L

Reputation: 6249

You are trying to echo an array, change <?= to <?php in:

<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>

Upvotes: 3

Related Questions