0

Suppose I have just inserted row on table test as like Test::create($inputs);,which has auto increment primary key field id.How to get id of row that I have just inserted using laravel 4.2/5 ?

Sumit
  • 1,235
  • 4
  • 17
  • 29
  • check out this http://stackoverflow.com/questions/21084833/laravel-get-last-insert-id-using-eloquent – jewelhuq Sep 24 '15 at 00:04
  • @jewelhuq ,this uses save() method which needs to set value for each field one by one.However I want to use create() method to insert that takes array of inputs at once to insert data and get recently inserted row's id – Sumit Sep 24 '15 at 00:14
  • You may try DB::getPdo()->lastInsertId(); – jewelhuq Sep 24 '15 at 00:20

3 Answers3

1

The following snippet:

$test = Test::create($inputs)

Will enable you to use $test->id.

user2094178
  • 9,204
  • 10
  • 41
  • 70
0

You can get the inserted id by using the example

$inserted_id = Test::create($inputs)->id;
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
-1

You can use the following line to fetch last insert id while save

after save the record you need to use $test->id;

  • Your answer is very confusing. Please consider editing using a more clear way of expressing your suggestion. – tomab Sep 24 '15 at 09:11