you need to add renderer path in column like this way.
$this->addColumn(
'store_id',
[
'header' => __('Store View'),
'index' => 'store_id',
'filter' => false,
'sortable' => false,
'renderer' => 'Vendor\Module\Block\Adminhtml\Grid\Renderer\WebsiteId.php',
'column_css_class' => 'col-type',
'header_css_class' => 'col-type',
]
);
And then create WebsiteId.php
WebsiteId.php
<?php
namespace Vendor\Module\Block\Adminhtml\Grid\Renderer;
use Magento\Backend\Block\Context;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\DataObject;
use Magento\Store\Model\StoreManagerInterface;
use Vendor/Module/Model/Modelfactory;
class WebsiteId extends AbstractRenderer
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
protected $modelFactory;
public function __construct(
Context $context,
array $data = []
)
{
$this->storeManager = $storeManager;
$this->modelFacotry = modelFactory;
parent::__construct($context, $data);
}
public function render(DataObject $row)
{
$slideId = $row->getSlideId();
$model = $this->modelFactory->create();
$storeIds = $model->getStores($slideId);
//Here Stores id's are returned in the form of array.
$storeName = [];
foreach($storeIds as $items) {
$storeName[] = $this->getStoreCodeById($items);
}
return implode(",", $storeName);
}
/**
* Get Store code by id
*
* @param int $id
*
* @return string|null
*/
public function getStoreCodeById(int $id): ?string
{
try {
$storeData = $this->storeManager->getStore($id);
$storeCode = (string)$storeData->getCode();
} catch (LocalizedException $localizedException) {
$storeCode = null;
$this->logger->error($localizedException->getMessage());
}
return $storeCode;
}
}
In your model class write this function
public function getStores($slideId){
return $this->getResource()->getStores($slideId);
}
In your resource model write this function
public function getStores($slideId){
$connection = $this->getConnection();
$select = $connection->select()->from(
$this->getTable('your_table_name'),
['store_id']
)->where(
'slide_id = ?',
(int) $slideId
);
return $connection->fetchCol($select);
}
It returns you the array of matching records store ids.