Hello Devs, 

In this tutorial, we will learn Laravel Eloquent insertGetId() Query Example

The use of insertGetId query in laravel used to insert record and get insert record id. If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:

Follow this step by step guide below. 


Query 1

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
	$id = DB::table('users')->insertGetId(
            ['email' => 'john@example.com','name' => 'john']
        );
}


Query 2

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $id = User::insertGetId(
	        ['email' => 'john@example.com','name' => 'john']
	    );
}


May this example help you.