How to get last inserted id in laravel 5?By Hardik Savani | February 6, 2016 | | 47782 Viewer | Category : Laravel
Its very common need to get last insert id in programming field, if you are working on laravel framework and you want to fetch last created id, i mean max id, then you do easily.laravel provide method to get last inserted id.
If you are working with DB::table() then you have to use insertGetId() for insert data into database. this function always return inserted record id(primary key).it is very simple way to get id, in following example you can see and undestand more:
public function getLastInsertedIdDB()
$id = DB::table('items')->insertGetId(['name'=>'hello']);
But, if you are insert record using laravel Model then it is pretty simple to get without using any special function like insertGetId(), in following example you can see and how to use i create method.
public function getLastInsertedIdModel()
$create = Item::create(['name'=>'hello']);
- How to check file is exists or not in PHP?
- PHP - Input multiple tags with dynamic autocomplete example
- Laravel 5.3 new feature and update - $loop variable
- How to check word exist in string or not in PHP?
- How to integrate google map using gmaps.js in php example?
- Laravel 5 - How to create custom error page with example
- How to group by multiple columns in Laravel Query Builder?
- Laravel 5 - How to get route parameters in your route middleware?