I want to retrieve the last and first item from model collection in magento 2, in magento 1.9 i can simply retrieve it like this :
$lastItem = $collections->getLastItem();
$firstItem = $collections->getFirstItem();
is there a way i can do the same in magento 2, without using foreach loop like this:
$collections = $this->_objectManager->create('Namespace\Module\Model\Test')->getCollection();
$i=0;
foreach($collections as $collection){
if($i == 0){
$firstItem = $collection;
$i++;
} else{
$lastItem = $collection;
}
}
$lastItem = $collections->getLastItem(); and $firstItem = $collections->getFirstItem();
is not working?