I have databse table with data.My model was created in one module and i want to get table data in another module's block class, so that i can render those data into my phtml. table name:form columns:id,name,email module that contains model class:form
1 Answer
You can use below way. Please add below code in your another module's block file. And change model collection according to your model. You can access your model in any another module.
<?php
namespace PackageName\Module\Block;
class ListManufacturer extends \Magento\Framework\View\Element\Template
{
protected $_manufacturers;
protected $_manufacturerCollection;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Zmage\Manufacturer\Model\ResourceModel\Manufacturer\CollectionFactory $manufacturerCollection,
array $data = []
) {
$this->_manufacturerCollection = $manufacturerCollection;
parent::__construct($context, $data);
}
public function getManufacturers()
{
if (empty($this->_manufacturers)) {
$this->_manufacturers = $this->_manufacturerCollection->create();
}
return $this->_manufacturers;
}
}
Above getManufacturers() function that you need to call in phtml file and get data with the use of foreach like below.
<?php $manufacturerCollection = $block->getManufacturers(); ?>
<?php if($manufacturerCollection->count()): ?>
<?php foreach($manufacturerCollection as $manufacturer): ?>
<?php echo $manufacturer->getTitle(); ?>
<?php endforeach; ?>
<?php endif; ?>
You can change function name according to your need. According to above code you need get your table's field data.
-
1