How to get first item of collection in magento 2

I am trying

    $infoColl = $this->_customFactory->create()->getCollection();
    $infoColl->addFieldToFilter('session_id', $id);
    $infoColl->addFieldToFilter('waiting_list', '');

but its not working

  • If I were to ask you the same thing, what would to say to me?
    – nevvermind
    May 9, 2016 at 13:35
  • Please share more details
    – Amit Bera
    May 9, 2016 at 13:36
  • Please, add more your code lines. May 9, 2016 at 13:49
  • @nevvermind if i know the answer than i will post answer or if i have question about your question than ask it :-)
    – Newbie
    May 9, 2016 at 13:58
  • 1
    your code seems correct to me, did you try to call specific data. I mean something like this $infoColl->getFirstItem()->getName(); May 10, 2016 at 4:43

5 Answers 5


Make sure there is data in your collection. The function will return the first item object from your collection and then you can use it directly.

 $collection = $this->_customFactory->create()->getCollection()
                    ->addFieldToFilter('session_id', $id)
                    ->addFieldToFilter('waiting_list', '');

     $data = $collection->getFirstItem();
  • 2
    If you pull first element of a collection with getFirstItem() you will get a model every time. You do not have to check if collection has element, you only need to check if the model has Id This is a quite handy feature, if you have to create an element if not exist: you can save the model creation
    – Mammouth
    Jan 30, 2018 at 18:34

You can use like below:

$infoColl = $this->_customFactory->create()->getCollection();
$infoColl->addFieldToFilter('session_id', $id);
$infoColl->addFieldToFilter('waiting_list', '');
$infoCollFirstItem = $infoColl->getFirstItem();
$infoCollFirstItemData =  $infoCollFirstItem->getData();

And by $infoCollFirstItemData data array you can use all columns of the first item of the collection.

You can also use fetchItem() function for fetch items/rows of the collection one by one.

This is a preferable approach and even faster, as I find at below url:


The example code:

$customers = $this->_customFactory->create()->getCollection();
$object=$customers->fetchItem(); //for get first Item/row
//for Iterating collections
        //do something

Before call getFirstItem(), try to load our collection $infoColl->load()

Update: @ MohammadMujassam comment: We need to get specific data, like: $infoColl->getFirstItem()->getName();

  • The collection gets loaded automatically when you call getFirstItem().
    – nevvermind
    May 9, 2016 at 15:58
  • @ nevvermind vendor/magento/module-sales/Model/Order.php there is a method loadByIncrementIdAndStoreId, it also tries to load collection before getFirstItem(). Please, help us to understand about this. Thanks you. May 9, 2016 at 16:27
  • Understand what exactly? Every call to getFirstItem() will call load() for you. This happens on every DB collection. So it's redundant (not wrong) to do $coll->load(); $coll->getFirstItem();. So your answer cannot explain the problem here.
    – nevvermind
    May 9, 2016 at 17:45

You can use fetchItem() method instead


I had same issue and resolve it by below code.

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

Hope It helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.