Tutorialsplane

Magento 2 Collection get First Item


Magento 2 Collection get First Item(Single row)– It is very simple to get the first item in Magento 2. Here in this tutorial we are going to explain how you can get the single(first item) from the collection .


Magento 2 Collection get First Item | Element | Limit 1 | Single Row Example

You can get the first item simply using the ->getFirstItem() on your collection-

Magento 2 Collection get First Item | Result | Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();   
$firstItem = $objectManager->create('Mypackage\MyModule\Model\MyData')
                    ->getCollection()
                    ->getFirstItem();

The above example will give you the first item from the collection. You can sort the collection to get the last item.