1
votes

I'm trying to get the user profile image of the author of the content inside the view , my view is channel I created but I get this error on my view

Undefined variable: queryAvatar

on my view I did

//Yii2 View code

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Channel */

$this->title = $model->Channel_name;
?>
<div class="channel-view">

    <h1><?= Html::encode($this->title) ?></h1>
    <div><?php echo  $queryAvatar; ?></div> // THE LINE I ADD

    <?php if($model->uid == Yii::$app->user->id):?>
    <p>
        <?= Html::a('Update', ['update', 'id' => $model->Channel_id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->Channel_id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>
    <?php endif;?>
</div>

and on my controller I created this function

//function on my controller

 public function actionChannel($id)
        {
            $cchannel = new Channel();
            $queryAvatar = (new \yii\db\Query())

            ->select(['uavatar'])
            ->from('userlogin')
            ->where(['uid' => $cchannel->uid])
            ->All();

            return $this->render('channel', [
                'model' => $this->findModel($id),
                'queryAvatar '=> $queryAvatar ,
            ]);
        }
2
In your code you define $cchannel as new model, so it is empty and $cchannel->uid are empty. - Sfili_81
yes but i'm getting error : Undefined variable: queryAvatar - Bynd
Because the query return empty value , the where clause is something like that : WHERE uid = '' - Sfili_81
You must define $cchannel = $this->findModel($id) instead of $cchannel = new Channel(); - Sfili_81
i'llanswer so you can vote me :) - Sfili_81

2 Answers

3
votes

in your controller you define an empty model so your $cchannel->uid is empty.

You must define first your model like this

 public function actionChannel($id)
        {
            $cchannel = $this->findModel($id);
            $queryAvatar = (new \yii\db\Query())

            ->select(['uavatar'])
            ->from('userlogin')
            ->where(['uid' => $cchannel->uid])
            ->All();

            return $this->render('channel', [
                'model' => $cchannel ,
                'queryAvatar '=> $queryAvatar ,
            ]);
        }
1
votes

The problem can be caused by space when you created array of variable before giving to view.

return $this->render('channel', [
                'model' => $this->findModel($id),
                'queryAvatar  '=> $queryAvatar ,
            ]);

remove space from 'queryAvatar ' Like this:

return $this->render('channel', [
                    'model' => $this->findModel($id),
                    'queryAvatar'=> $queryAvatar ,
                ]);

Another problem can be caused by partial view in side view. For example when you call view:channel from controller inside it may be you view called like this.

<?=$this->render('your_view')?>

So, you should pass your variable to this view also like this.

<?=$this->render('your_view',['queryAvatar'=>$queryAvatar])?>

And finally you cannot echo the varable $queryAvatar beacuse it is array of objects. Therefore use the function print_r();

Like this.

   <div><?php print_r($queryAvatar); ?></div> // THE LINE I ADD