{"id":1111,"date":"2023-09-22T13:40:39","date_gmt":"2023-09-22T13:40:39","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1111"},"modified":"2023-09-30T13:41:12","modified_gmt":"2023-09-30T13:41:12","slug":"how-to-get-the-last-inserted-id-using-laravel-eloquent","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/","title":{"rendered":"How to Get the Last Inserted Id Using Laravel Eloquent ?"},"content":{"rendered":"\n<p>In this tutorial i&#8217;ll guide, you will learn how to get the last insert id in Laravel.<\/p>\n\n\n\n<p><strong>1. Using Eloquent Save Method<\/strong><\/p>\n\n\n\n<p>When you create or update a model in Laravel, you can easily get the ID of the last inserted record by using the&nbsp;<code>save()<\/code>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$user = new User();\n$user-&gt;name = 'John Doe';\n$user-&gt;email = 'johndoe@example.com';\n$user-&gt;save();\n\n$lastInsertId = $user-&gt;id;<\/code><\/pre>\n\n\n\n<p><strong>2. Using Elogquent Create Method<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$user = User::create(&#91;'name' =&gt; 'John Doe', 'email' =&gt; 'johndoe@example.com']);\n$lastInsertId = $user-&gt;id;<\/code><\/pre>\n\n\n\n<p><strong>3. Using DB Facade<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$lastInsertId = User::insertGetId(&#91;'name' =&gt; 'John Doe', 'email' =&gt; 'johndoe@example.com']);<\/code><\/pre>\n\n\n\n<p><strong>4. Using getPDO() Method<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$user = DB::table('users')-&gt;insert(\n    &#91; 'name' =&gt; 'John Doe', 'email' =&gt; 'johndoe@example.com' ]\n); \n$userId = DB::getPdo()-&gt;lastInsertId();.\n\ndd($userId);<\/code><\/pre>\n\n\n\n<p>That&#8217;s it for now. I hope this little tip helps you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial i&#8217;ll guide, you will learn how to get the last insert id in Laravel. 1. Using Eloquent Save Method When you create or update&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[461,462,3,460],"class_list":["post-1111","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-eloquent","tag-eloquent-laravel","tag-laravel","tag-laravel-get-value"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Get the Last Inserted Id Using Laravel Eloquent ? - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Get the Last Inserted Id Using Laravel Eloquent ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"In this tutorial i&#8217;ll guide, you will learn how to get the last insert id in Laravel. 1. Using Eloquent Save Method When you create or update...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-22T13:40:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-30T13:41:12+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"How to Get the Last Inserted Id Using Laravel Eloquent ?\",\"datePublished\":\"2023-09-22T13:40:39+00:00\",\"dateModified\":\"2023-09-30T13:41:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/\"},\"wordCount\":80,\"commentCount\":0,\"keywords\":[\"Eloquent\",\"Eloquent-laravel\",\"laravel\",\"laravel-get-value\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/\",\"name\":\"How to Get the Last Inserted Id Using Laravel Eloquent ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"datePublished\":\"2023-09-22T13:40:39+00:00\",\"dateModified\":\"2023-09-30T13:41:12+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-get-the-last-inserted-id-using-laravel-eloquent\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Get the Last Inserted Id Using Laravel Eloquent ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\\\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/amitsthakurs\\\/\",\"https:\\\/\\\/www.instagram.com\\\/amits_thakurs\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/amits-thakurs\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/amits_thakurs\"],\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/author\\\/amit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Get the Last Inserted Id Using Laravel Eloquent ? - DevOps Freelancer","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/","og_locale":"en_US","og_type":"article","og_title":"How to Get the Last Inserted Id Using Laravel Eloquent ? - DevOps Freelancer","og_description":"In this tutorial i&#8217;ll guide, you will learn how to get the last insert id in Laravel. 1. Using Eloquent Save Method When you create or update...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-09-22T13:40:39+00:00","article_modified_time":"2023-09-30T13:41:12+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"How to Get the Last Inserted Id Using Laravel Eloquent ?","datePublished":"2023-09-22T13:40:39+00:00","dateModified":"2023-09-30T13:41:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/"},"wordCount":80,"commentCount":0,"keywords":["Eloquent","Eloquent-laravel","laravel","laravel-get-value"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/","name":"How to Get the Last Inserted Id Using Laravel Eloquent ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-09-22T13:40:39+00:00","dateModified":"2023-09-30T13:41:12+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-get-the-last-inserted-id-using-laravel-eloquent\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Get the Last Inserted Id Using Laravel Eloquent ?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1111"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1111\/revisions"}],"predecessor-version":[{"id":1113,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1111\/revisions\/1113"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}