1

I have created a custom module: Vendor_Faq

My module structure is:

Vendor
└───Faq
    │   registration.php
    │
    ├───etc
    │       module.xml
    │
    ├───Model
    │   │   Custom.php

    │   │
    │   └───ResourceModel
    │       │   Custom.php
    │       │
    │       └───Custom
    │               Collection.php
    │
    ├───Setup
    │       InstallSchema.php
    │       
    │
    └───view
        └───frontend
            │___Template
            │       Faq
                        Index.php
            ├───layout
            │       faq_faq_index.xml
            │
        

After the creation of the module, I have written the below code but it's not working.

Below is code with a folder structure:

/block/Faq/Index.php

<?php

namespace Vendor\Faq\Block\Faq;
use Vendor\Faq\Block\BaseBlock;

class Index extends BaseBlock
{
    //public $hello='Hello World';

    protected $mymodulemodelFactory;
    public function __construct(
    \Vendor\Faq\Model\ResourceModel\Custom\CollectionFactory $mymodulemodelFactory, 
    )
    {
        $this->mymodulemodelFactory = $mymodulemodelFactory;
    }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->create();
        return $collection;
    }


}

AND

get collection data folder structure:

/View/Frontend/Template Faq.Index.php

echo $block->getCollection();

but it's not working. After running the code it displays the blank page.

Please suggest a solution?

3
  • plz let me know if you have issue Commented Jun 23, 2017 at 5:52
  • just set, $collection = $this->mymodulemodelFactory->create()->getCollection() in your Block file; Commented Jun 23, 2017 at 5:58
  • please show your BaseBlock.php file code Commented Jun 23, 2017 at 6:25

3 Answers 3

0

You can get data using foreach of your collection,

class Index extends BaseBlock
{
    //public $hello='Hello World';

     protected $mymodulemodelFactory;
     public function __construct(
       \Ashore\Faq\Block\Context $context,
       \Ashore\Faq\Model\CustomFactory $mymodulemodelFactory
     )
     {
        $this->mymodulemodelFactory = $mymodulemodelFactory;
        parent::__construct($context);
     }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->create()->getCollection()->addAttributeToSelect('*');
        return $collection;
    }
}

in Block file,

$collection = $block->getCollection();
foreach($collection as $moduledata){
    echo "<pre>";print_r($moduledata->getData());
}

Remove var folder and check.

15
  • Not working ...dislpay blank page
    – Devidas
    Commented Jun 23, 2017 at 5:57
  • plz check Block file its correct or not
    – Devidas
    Commented Jun 23, 2017 at 5:58
  • not working -display blank page ....plz give me different solution to collection of module data
    – Devidas
    Commented Jun 23, 2017 at 6:05
  • i have set, $collection = $this->mymodulemodelFactory->create()->getCollection() in my Block file but its not working
    – Devidas
    Commented Jun 23, 2017 at 6:07
  • 1
    Don't place model/helper/resourcemodel functions in controller files
    – treyBake
    Commented Nov 24, 2017 at 15:12
1
<?php

namespace Vendor\Faq\Block\Faq;

use Magento\Framework\App\RequestInterface;
use Vendor\Faq\Block\BaseBlock;
use Vendor\Faq\Model\ResourceModel\Custom\CollectionFactory;

class Index extends \Magento\Framework\View\Element\Template 
{
    protected $mymodulemodelFactory;
    protected $request;

    public function __construct(
    CollectionFactory $mymodulemodelFactory,
    \Magento\Catalog\Block\Product\Context $context,
    RequestInterface $request,
    array $data = [] 
    )
    {
        parent::__construct($context, $data);
        $this->_request = $request;
        $this->mymodulemodelFactory = $mymodulemodelFactory;
    }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->getCollection();
        return $collection;
    }
}

Hey @Devidas I hope It's Work For you

0

Try to below code

namespace Vendor\Modulename\Controller\Custom;

Class Index extends \Magento\Framework\View\Element\Template
{
    protected $mymodulemodelFactory;
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Vendor\Modulename\Model\CustomFactory $mymodulemodelFactory
    )
    {
        $this->mymodulemodelFactory = $mymodulemodelFactory;
        parent::__construct($context);
    }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->create()->getCollection()->addAttributeToSelect('*');
        return $collection;
    }
}

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.