0

I have created the custom module and inserted all the required fields detail in the database. Now, I want to list all rows in below file path:

I have tried with the $objectManager, but direct call $objectManager in phtml file is not correct way.

Before post this question I have go through the below answers, but when I run I getting a blank screen.

How can i get collection of custom module in magento 2

Magento 2 : How to get custom module collection in module template file

app\design\frontend\Vendor\Theme\Magento_Theme\templates\html/home_page.phtml

<div class="" data-section-name="home">
  <div class="header-section">
    <?php $collection = $block->getCustomoptionmanagerList();
        echo '<pre>'; print_r($collection); die;
    ?>

  </div>
</div>

  <div class="back-top-button"><a href="javascript:void(0)"><i class="top-arrow"></i> TOP</a></div>

Model Class code file

app\code\Vendor\Customoptionmanager\Model/Customoptionmanager.php

<?php

namespace Vendor\Customoptionmanager\Model;

class Customoptionmanager extends \Magento\Framework\Model\AbstractModel
{
    protected function _construct()
    {
        $this->_init('Vendor\Customoptionmanager\Model\ResourceModel\Customoptionmanager');
    }
}

Block file Code:

<?php

use Vendor\Customoptionmanager\Model\CustomoptionmanagerFactory;
use Magento\Framework\Data\Collection;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class CustomoptionmanagerList extends Template
{
    protected $_modelCustomoptionmanagerFactory;

    public function __construct(Context $context, 
            CustomoptionmanagerFactory $CustomoptionmanagerList, 
            array $data = [])
        {
            $this->_modelCustomoptionmanagerFactory = $CustomoptionmanagerList;
           parent::__construct($context, $data);
        }

   public function getCustomoptionmanagerList()
   {
      $optionCollection = $this->_modelCustomoptionmanagerFactory->create()->getCollection();
       return $optionCollection;
   }
}
?>
0

You are using HomepageFactory in dependency injection but you have not put it in use anywhere . It seems like you want to inject instance of Vendor\Customoptionmanager\Model\CustomoptionmanagerFactory which should be done using CustomoptionmanagerFactory in dependency injection .

 public function __construct(Context $context, 
            CustomoptionmanagerFactory $CustomoptionmanagerList, 
            array $data = [])
        {
            $this->_modelCustomoptionmanagerFactory = $CustomoptionmanagerList;
           parent::__construct($context, $data);
        }
  • I have updated the code after your answer, but not solved issue. – mageDev0688 Apr 4 '17 at 11:28
  • Can you specify the error you're getting? – Vivek Kumar Apr 4 '17 at 12:32
  • I have solved this issue and now it's working fine, Thanks – mageDev0688 Apr 5 '17 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.