How to load the model in magento 2 like
Mage::getModel('catalog/product')->load()
as we used to get in magento 1.
It is recommended to use dependency injection rather than directly using object Manager. Example: In your block file you can use following code to return product collection:
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
return $collection;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With