laravel 6

Get Last id Insert in Laravel

beberapa cara untuk mendapatkan last id insert in laravel

  1. Menggunakan method insertGetId()
$id = DB::table('users')->insertGetId(
    [ 'name' => 'first' ]
);

dd($id);

2. Menggunakan method lastInsertId()

DB::table('users')->insert([
    'name' => 'TestName'
]);
$id = DB::getPdo()->lastInsertId();
dd($id);

3. Menggunakan method create()

$data = User::create(['name'=>'first']);
dd($data->id);

4. Menggunakan method save()

$data = new User;
$data->name = 'Test';
$data->save();
dd($data->id)

saya sudah gunakan method save() dan method insert() dan hasilnya work for me.

Post Author: @danangnurfauzi

Website Developper. CodeIgniter Framewok. Wordpress Enthusiast. Laravel Framework

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.