Image in gridview in yii2
Try like,
array(
'format' => 'image',
'value'=>function($data) { return $data->imageurl; },
),
And in your model,
public function getImageurl()
{
return \Yii::$app->request->BaseUrl.'/<path to image>/'.$this->logo;
}
Don't know this is the right way or not.But this works for me.
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);
}