Get Model Collection

 In this article, we will discuss about how to create model and get model using Factory in Magento 2.

In Magento 2, Models are very useful for get model collection and  perform CRUD(Create, Read, Update and Delete) operations on database. Models are important part of MVC(Model View Controller) structure in Magento 2.

Please follow below example  or steps for create  model and get model collection using factory in Magento 2 Module.

Step 1 : Please create one custom module. I have created my custom module and Create Alltest.php model under the Model Folder  as follows:


      

<?php

namespace Mageniks\Alltest\Model;

class Alltest extends \Magento\Framework\Model\AbstractModel

{

  public function __construct(

        \Magento\Framework\Model\Context $context,

        \Magento\Framework\Registry $registry,

        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,

        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,

        array $data = []

  ) {

    parent::__construct($context, $registry, $resource, $resourceCollection, $data);

  }


  public function _construct()

  {

    $this->_init('Mageniks\Alltest\Model\ResourceModel\Alltest');

  }

}


=> In this model file, i have define the resource model class.

Step 2: Create Alltest.php in ResourceModel Folder as follows:

 

       

<?php

namespace Mageniks\Alltest\Model\ResourceModel;

class Alltest extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb

{

    public function _construct()

    {

       $this->_init('alltest', 'alltest_id');

    }

}

=> In this resource model file i have declare table name "alltest" with primary key "alltest_id"

Step 3 : Create Collection.php in ResourceModel/Alltest Folder as below: 

 

<?php 

namespace Mageniks\Alltest\Model\ResourceModel\Alltest;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

{

      public function _construct()

      {

         $this->_init('Mageniks\Alltest\Model\Alltest','Mageniks\Alltest\Model\ResourceModel\Alltest');

      }


=> In this collection file, i have define both model and resource model file.

Step 4: Model files created successfully in our module. Now you can get model collection using Factory class object.

 We will discuss about Factory in our next article deeply. In block Mageniks\Alltest\Block\Alltest we have create Factory object fot getting model collection.

 I have just add factory word behind the model class name like "AlltestFactory". The below Alltest block class shows the AlltestFactory instance using the constructor.

 When we call the create() method on a factory object, it's gives you an instance of its specific class. Please check below code.

       

<?php

namespace Mageniks\Alltest\Block;


class Alltest extends \Magento\Framework\View\Element\Template

{

    protected $_moduleCollectionFactory;

    public function __construct(

        \Magento\Framework\View\Element\Template\Context $context,

        \Mageniks\Alltest\Model\AlltestFactory $moduleCollectionFactory,

        array $data = []

    ) {

        

  $this->_moduleCollectionFactory   = $moduleCollectionFactory;

  parent::__construct($context, $data);

       

    }

 

 public function getModuleCollection() 

 {

  $productCollection = $this->_moduleCollectionFactory->create()->getCollection();

  return $productCollection;

 }

   

}


Step 5:  For getting our module collection i have create one function  getModuleCollection()  in block. Let's call this block function from template file. Please check below code.


       

<?php

$collection = $block->getModuleCollection();


In template file, you can access block function using $block and get collection easily.

That's it.