Magento 2 Get Last Insert Id Sometimes we need to get the last inserted id in database table. It is very simple to get the last inserted id in Magento 2. It is a little bit different from Magento 1. Here in this tutorial we are going to explain how you can get the last inserted row id in Magento 2.
Magento 2 Get Last Insert Id | Last Record From Model Example
Here is a simple example to get the last inserted id –
Magento 2 Get Last Insert Id Example:
$model = $this->_objectManager->create('Ecom\HelloWorld\Model\Question'); $model->setTitle('Simple Question'); $model->setDescription('Question Description'); $model->save(); $lastInsrtedId = $model->getId(); |
The above example will give you the last Inserted id from the database.