I want to get the last inserted id to my block without adding new data into the table. I want to use this value to automatically filled in my phtml file. I tried getId and it's not working. I just need the last id. Can anyone help?

  • uhmm ok, but why? normally, you would set the ID column as autoincrement so you don't have to worry about 'what is the next ID to insert here'.
    – Diana
    Jun 23, 2021 at 12:52
  • actually i want auto-increment in two colums and when i tried to create using dbschema,its saying that auto icrement is set only for one column. so thats why im going for another method:)
    – mage
    Jun 23, 2021 at 15:46
  • i see. well, in that case, you either call last_insert_id() method (w3schools.com/Sql/func_mysql_last_insert_id.asp) ooooor you create a stored procedure that you call in order to increment the second column :D :D
    – Diana
    Jun 23, 2021 at 16:14

4 Answers 4


Try below code

protected $_resource;
public function __construct(
   \Magento\Framework\App\ResourceConnection $resource
   ) {
   $this->_resource = $resource;

$connection = $this->_resource->getConnection();
$yourLastAddedId = $connection->lastInsertId('your_table_name');

or use below

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('your_table_name');
$yourLastAddedId = $connection->lastInsertId($tableName);

hope its helps you:)

  • sorry, it didn't work for me
    – mage
    Jun 24, 2021 at 15:46
  • $this->getConnection() is not accessible on this Jul 5, 2021 at 7:39
  • 1
    @zaheerabbas answer updated Jul 5, 2021 at 12:42
  • @GohilRajesh it will always return 0.As there isn't any function in that class Jul 6, 2021 at 5:30
  • check another added code. try to first achive through object manager Jul 6, 2021 at 6:14

i got the answer by creating helper class

namespace vendor\module\Helper;

use vendor\module\Model\ResourceModel\Collection_Folder\CollectionFactory;
class Helper extends \Magento\Framework\Url\Helper\Data

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        CollectionFactory $modelFactory
    ) {
        $this->modelFactory = $modelFactory;
        return parent::__construct($context);
    public function getLatestid()
        $model = $this->modelFactory->create(); 
        $data = $model->load();
        foreach ($data as $item) {
            $id = $item->getData('id');
        return $id;


This way we can get last inserted Id..


You can achieve this with a custom SQL statement

protected $resourceConnection;
public function __construct(
   \Magento\Framework\App\ResourceConnection $resourceConnection,
   ) {
   $this->resourceConnection = $resourceConnection;

 $connection = $this->resourceConnection->getConnection();
 $sql = "select id FROM customtable order by id desc limit 1";
 $latestIdResults= $connection->fetchAll($sql);
 $latestId = 0;

 foreach($latestIdResults as $id){
          $latestId = $id["id"];                        

$latestId will be your lastest incremented id :) There are mysql functions for this but this is one way to do it if your keys and tables are heavily customized.


What about

$lastId = $this->resource->getConnection()->fetchOne(
                    ->from('my_table', ['id'])
                    ->order('id DESC')

Your Answer

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

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