Reputation: 4508
I'm trying to get data from DB and show it in menu.
echo NavX::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
array_map(function ($model) {
return [
'label' => $model['param2'],
'url' => ["/users/{$model['param3']}"],
];
}, Model::find()->where(['userID' => 32])->asArray()->all()),
],
]);
But unfortunately this code is wrong, error says:
Array to string conversion
Is there any other way to fix it ?
Upvotes: 2
Views: 1461
Reputation: 9728
My guess, without testing:
echo NavX::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
array_map(function ($model) {
return [
'label' => $model['param2'],
'url' => "/users/{$model['param3']}", // <--- string, not array!
];
}, Model::find()->where(['userID' => 32])->asArray()->all()),
],
]);
Upvotes: 1
Reputation: 33538
One of the "Yii" way to do it will be using built-in ArrayHelper
:
use yii\helpers\ArrayHelper;
...
$models = Model::find()->where(['userID' => 32])->asArray()->all();
$items = ArrayHelper::toArray($models, [
'app\models\Model' => [
'label' => function ($model) {
// Add label generation code here
},
'url' => function ($model) {
// Add url generation code here
}
],
]);
Then just pass the $items
to your view with menu.
Official docs:
Upvotes: 0