How to put an image in GridView in yii2?I have the following code. But its not displaying the image as it is not giving any image url. Where to put the image url?
I used to use 'raw' format for this type of work.
[
"attribute": "image",
"format": "raw",
"value": function($model){
return ($model->image) ? Html::img("/path-to-img-location" . $model->image) : false;
}
]
if image exists then image is displayed otherwise it is blank.
Use this:
[
'attribute' => 'image',
'format' => 'html',
'value' => function ($data) {
return Html::img(Yii::getAlias('@web').'/images/'. $data['image'],
['width' => '70px']);
},
],
Yii 2 has built-in helper for building urls. You can bulld the url to image by path too (by passing second parameter $scheme
).
So I recommend using this:
GridView:
use yii\helpers\Url;
[
'format' => 'image',
'value' => function ($model) {
return $model->getImageUrl();
},
],
Model:
public function getImageUrl()
{
return Url::to('@web/path/to/logo/' . $this->logo, true);
}