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;
   }
}
?>
1
  • Inject a view model to the block that has template home_page.phtml: devdocs.magento.com/guides/v2.4/extension-dev-guide/…. You can also use a block, but highly not recommended. In any case, the problem here is that you don't have your model available to the template either via a block or view model. You can set the block or view model in the layout.
    – David Tay
    May 31, 2021 at 14:09

1 Answer 1

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);
        }
3
  • I have updated the code after your answer, but not solved issue. Apr 4, 2017 at 11:28
  • Can you specify the error you're getting? Apr 4, 2017 at 12:32
  • I have solved this issue and now it's working fine, Thanks Apr 5, 2017 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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