How to get last inserted id in laravel 5?By Hardik Savani | February 6, 2016 | | 19823 Viewer | Category : Laravel
Share this post:
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 set ANDROID_HOME environment variable in ubuntu?
- Laravel 5 - Barcode generator example using milon/barcode package
- How to check request method is GET or POST in Laravel?
- How to add LIKE query in Elasticsearch?
- How to resolve - No supported encrypter found. The cipher and / or key length are invalid. in Laravel 5
- PHP Codeigniter - Ajax Infinite Scroll Pagination Example
- Ajax Image Upload using PHP and jQuery Example from scratch
- Bootstrap daterangepicker example code with demo using daterangepicker.js