In Magento 2, how can I get a custom model that is created in a custom module (not a Magento 2 native module)? I know I can use object manager for getting and creating, and I know how do this for Magento models, for example, to get the Mage::getModel('sales/order') I can use: $om->get('Magento\Sales\Model\Order') . How can I get a custom model "custommodel" created in module "modulename" ?


You can get the model by injecting it in the constructor.

like :

if your model is


You should use inject in the constructor like

public function __construct(
    <vendor_name>\<module_name>\Model\CustomModelFactory $customModelFactory
) {
    $this->customModelFactory = $customModelFactory;

now you can get customModel object by using $this->customModelFactory->create().

But the more appropriate way to access a model is by using a repository.

So follow the repository pattern magento 2 follows and then inject the repository in the constructor.

Now using repository you can get model.

For more info on repository pattern refer : http://alanstorm.com/magento_2_understanding_object_repositories/

| improve this answer | |

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.