How to get last inserted id in laravel 5?By Hardik Savani | February 6, 2016 | | 31235 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 get ckeditor value with tags on ajax-submit ?
- How to add LIKE query in Elasticsearch?
- AngularJS - How to call function on page load example with demo?
- How to copy one table data into another table using Mysql Query?
- How to implement Email Verification with Activation Code example from Scratch in Laravel 5.2 ?
- How to get ip address using php code?
- How to send mail using zoho smtp server in PHP Laravel?
- Jquery notification popup box example using toastr JS plugin with demo