How to Get Last Inserted ID in Laravel

  Jan 2024
  ITSolutionsGuides
  Category: Laravel
How to Get Last Inserted ID in Laravel

Welcome To ITSolutionsGuides,

Laravel provides various methods to retrieve the ID of the record you recently inserted into your database create(), insertGetId(). This is the simplest and recommended approach. When you use the create() method on your model to insert a new record, it automatically returns the newly created record's ID. While less common in Laravel due to simpler options, you can still access the underlying PDO connection's lastInsertId() method to retrieve the last inserted ID. However, this approach requires more manual database interaction. Lets see How to Get Last Inserted ID in Laravel using the it solutions guides tutorial.

Let's Start Coding

Example : 1)

This is the simplest and recommended approach. When you use the create() method on your model to insert a new record, it automatically returns the newly created record's ID

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $create = User::create([
                            'name' => 'ITSolutionsGuides',
                            'email' => 'itsolutionsguides@gmail.com',
                            'password' => '123456789'
                        ]);
  
        $lastInsertID = $create->id;
          
        dd($lastInsertID);
    }
}

Let's Start Coding

Example : 2)

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use DB;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $lastInsertID = DB::table('users')->insertGetId([
                            'name' => 'ITSolutionGuides',
                            'email' => 'itsolutionsguides@gmail.com',
                            'password' => '123456'
                        ]);
  
        dd($lastInsertID);
    }
}

Let's Start Coding

Example : 3)

While less common in Laravel due to simpler options, you can still access the underlying PDO connection's lastInsertId() method to retrieve the last inserted ID. However, this approach requires more manual database interaction.

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
       $data           = new User();
       $data->name     = "ITSolutionsGuides";
       $data->email    = "itsolutionsguides@gmail.com";
       $data->password = "123456789";
       $data->save();

       dd($data->id);;
    }
}

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides