不将变量从控制器传递到模板yii2

cbjzeqam  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(87)

我有控制器

public function actionIndex()
    {
        $rows = Players::find()->all();

        return $this->render('index', [
            'rows' => $rows
        ]);

    }

和查看
<h1><?=$listPlayer -> name?></h1>
错误=未定义的变量“$listPlayer”
我重新阅读了所有的文档和互联网上的所有指南,它对每个人都有效。除了我

UPD

控制器

public function actionIndex()
    {
        $rows = Players::find()->all();

        return $this->render('index', [
            'rows' => $rows
        ]);

    }

视图

<?php foreach ($rows as $listPlayer): ?>
            <h1><?= $listPlayer->name ?></h1>
        <?php endforeach; ?>
wtlkbnrh

wtlkbnrh1#

首先:<h1><?=$listPlayer -> name?></h1>-在开始/结束标签之间插入一些空格(如果你按原样粘贴)。
第二:定义$listPlayer。在你的操作中没有任何变量$listPlayer。您只有$行。你好,我是说-有一个球员名单,如果你需要一个名单(. ->all())。

相关问题