# Cakephp 3.0 how to get lastInsertId

**URL:** <https://discourse.cakephp.org/t/cakephp-3-0-how-to-get-lastinsertid/1453>\
**Category:** Uncategorized\
**Created:** [November 4, 2016, 11:25am UTC](https://discourse.cakephp.org/t/cakephp-3-0-how-to-get-lastinsertid/1453 "2016-11-04T11:25:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kanhaiya](https://avatars.discourse-cdn.com/v4/letter/k/43a26b/32.png) [@kanhaiya](https://discourse.cakephp.org/u/kanhaiya)\
**Post date:** [November 4, 2016, 11:25am UTC](https://discourse.cakephp.org/t/cakephp-3-0-how-to-get-lastinsertid/1453/1 "2016-11-04T11:25:40Z")

</div>

Dear Sir ,  
i am new to cakephp 3.x so i just want to know how to get inserted id after data save in controller.I have used $this-\>MyModel-\>lastInsertId() but this shows error unknown method.please help

---

<div class="post-metadata">

**Author:** ![ldoerksen](https://avatars.discourse-cdn.com/v4/letter/l/e9bcb4/32.png) [@ldoerksen](https://discourse.cakephp.org/u/ldoerksen)\
**Post date:** [November 4, 2016, 1:20pm UTC](https://discourse.cakephp.org/t/cakephp-3-0-how-to-get-lastinsertid/1453/2 "2016-11-04T13:20:49Z")

</div>

In Cake 3.x you get the Entity as a return value from the save operation:

$entity = $this-\>MyModel-\>newEntity($this-\>request-\>data())  
$result= $this-\>MyModel-\>save($entity); //Statement as result  
$id = $result-\>id;

This should work fine.

---

<div class="post-metadata">

**Author:** ![kanhaiya](https://avatars.discourse-cdn.com/v4/letter/k/43a26b/32.png) [@kanhaiya](https://discourse.cakephp.org/u/kanhaiya)\
**Post date:** [December 5, 2016, 7:16am UTC](https://discourse.cakephp.org/t/cakephp-3-0-how-to-get-lastinsertid/1453/3 "2016-12-05T07:16:42Z")

</div>

Thank you sir yest it works.
