How to load Model function in block in Magento 2 when overriding or extending a core block
In Magento 2, you should add a constructor with order factory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory, \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory, \Magento\Framework\Module\Manager $moduleManager, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Url $customerUrl, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory, array $data = [] ) { parent::__construct(...); $this->collectionFactory = $collectionFactory; |
use it in method hello
1 2 3 4 |
public function hello() { return $this->collectionFactory->create()->load(); } |
Source (