How to get last insert id in Eloquent ORM laravel

**** For Laravel ****

$user = new User();

$user->name = 'John';

$user->save();

//Getting Last inserted id

$insertedId = $user->id;

You could wrap your data in the insertGetId() method like this

$id = DB::table('table')->insertGetId( $data );

In this case the array $data would look something like this

$data = [ 'field' => 'data' , 'field' => 'data' ];

and if you’re using the DB façade you could just append the lastInsertID() method to your query.

$lastInsertedID = DB::table('table')->insert( $data )->lastInsertId();

Like the docs say: Insert, update, delete

"You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.

After saving or creating a new model that uses auto-incrementing IDs, you may retrieve the ID by accessing the object's id attribute:"

$insertedId = $user->id;

So in your sample:

$user = User::create($loginuserdata);
$insertedId = $user->id;

then on table2 it is going to be

$input['table2_id'] = $insertedId;
table2::create($input);