Laravel 6.2 Get Last Inserted ID

Today, We want to share with you php Laravel 6.2 Get Last Inserted ID.In this post we will show you get last inserted id laravel 6.2 query builder, hear for Laravel 6.2 – Get Last Inserted ID With Example we will give you demo and example for implement.In this post, we will learn about get id of new record laravel 6.2 with an example.

php Laravel 6.2 Get Last Inserted ID

There are the Following The simple About Get the Last Inserted Id Using Laravel 6.2 Eloquent Full Information With Example and source code.

As I will cover this Post with live Working example to develop Retrieve last insert id by Eloquent in Laravel 6, so the How to get last inserted id from table in Laravel 6 is used for this example is following below.

Read Also:  delete(unlink) file in directory in PHP

Laravel 6 Define a Routes

routes/web.php

<?php

Route::get('member', '[email protected]');

create a controller named MemberController

php artisan make:controller MemberController

Example 1 : Method insertGetId()

MemberController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Member;

class MemberController extends Controller
{
    public function create()
    {
        $id = DB::table('members')->insertGetId([
            'name' => 'rakesh bhanderi',
            'email' => '[email protected]'
        ]);

        return $id;
    }
}

Example 2 : Method save()

MemberController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Member;

class MemberController extends Controller
{
    public function create()
    {
        $member = new Member();
        $member->name = "jagdish donga";
        $member->email = "[email protected]";
        $member->save();

        return $member->id;
    }
}

Example 3 : Method create()

MemberController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Member;

class MemberController extends Controller
{
    public function create()
    {
        $input = ['name' => 'Md. Obydullah', 'email' => '[email protected]'];
        $member = Member::create($input);

        return $member->id;
    }
}

Example 4 : Method lastInsertId()

MemberController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Member;

class MemberController extends Controller
{
    public function create()
    {
        DB::table('members')->insert([
            'name' => 'Mr Tamilrokers',
            'email' => '[email protected]'
        ]);
        $id = DB::getPdo()->lastInsertId();

        return $id;
    }
}

Web Programming Tutorials Example with Demo

Read :

Read Also:  10 Useful PHP Filesystem Functions with Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about get saved id in laravel 6.2, save () and get id laravel 6.2.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.