Laravel 6.2 Get Last Inserted ID

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

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.

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:  jQuery Remove Item From Comma Separated string

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.

Read Also:  jQuery Find Nested Parent Child Elements HTML DOM

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *