I am trying to get the last id of my table, I want this to know what is the next number of my counter and to show it to the user, I've tried with the last()
method but I got this:
>>> $trans = Transferencia::last()
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::last()'
Is there other way to know this?
4 Ways to Get Last Inserted Id in Laravel :
Using insertGetId()
method:
$id = DB::table('users')->insertGetId([
'name' => 'first'
]);
Using lastInsertId()
method:
DB::table('users')->insert([
'name' => 'TestName'
]);
$id = DB::getPdo()->lastInsertId();
Using create()
method:
$data = User::create(['name'=>'first']);
$data->id; // Get data id
Using save()
method:
$data = new User;
$data->name = 'Test';
$data->save();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With