Display Image from database in yii2

hi,

how can I display images from database to gridviewcolumn in yii 2? help me please, my page display image name from database instead of image.

Thanks

It is only the names that are saved in the database. As long as these match real image files, they can be included in gridview or any other display based on $model. Assuming that your image attribute is called ‘yourimage’




//'yourimage', <--Replace this line  with array as below

 ['label' => 'Your Image',

   'format' => ['image',['width'=>'50']], 

   'value'=>function($model){

   return('@web/images/'.$model->yourimage);

   },

 ],



You can change width as needed.

I did it, but don’t work for me, here is my code


<?php


use yii\helpers\Html;

use yii\grid\GridView;


/* @var $this yii\web\View */

/* @var $dataProvider yii\data\ActiveDataProvider */


$this->title = Yii::t('app', 'Items');

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="items-index">


    <h1><?= Html::encode($this->title) ?></h1>


    <p>

        <?= Html::a(Yii::t('app', 'Create Items'), ['create'], ['class' => 'btn btn-success']) ?>

    </p>


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            //'item_id',

            'designation',

            'item_name',

            'unit_price',

            'stock',

            //'unit',

            //'item_image:ntext',         

          

            ['label' => 'Item Images',

               'format' => ['image',['width'=>'50']], 

               'value'=>function($model){

               return('@web/uploads/'.$model->item_image);

               },

            ],

            [

              'attribute' => 'cat_id',

              'value' =>  'cat.cat_name',

            ],




            ['class' => 'yii\grid\ActionColumn'],

        ],

        

    ]); ?>

</div>



Is


'@web/uploads/'.$model->item_image

return your image url properly?