3

Here is my code:

User::create([
    'name' => $request->name,
    'email' => $request->email,
    'cell_phone' => $request->cell_phone,
    'province_id' => $request->province,
    'city_id' => $request->city,
    'job' => $job,
    'company' => $company,
    'member_num' => $member_num,
]);

As you know, it inserts a new row in the database. Now I need to get the id of inserted row. How can I get that?


Noted that I know, I can do the same if I use $obj->save(), and the id will be $obj->id;. But I want to know how can I do that when I'm using create().

stack
  • 10,280
  • 19
  • 65
  • 117
  • $userDetials = User::create([ 'name' => $request->name, 'email' => $request->email, 'cell_phone' => $request->cell_phone, 'province_id' => $request->province, 'city_id' => $request->city, 'job' => $job, 'company' => $company, 'member_num' => $member_num, ]); – Ankur Tiwari Dec 13 '17 at 06:40
  • which version of Laravel you're talking about? – Chay22 Dec 13 '17 at 06:41

4 Answers4

3

You can get user inserted id using code

$userDetials = User::create([
     'name' => $request->name,
     'email' => $request->email,
     'cell_phone' => $request->cell_phone,
     'province_id' => $request->province,
     'city_id' => $request->city,
     'job' => $job,
     'company' => $company,
     'member_num' => $member_num,
]);
if(isset($userDetials->id))    
{
    //if inserted 
    return $userDetials->id;
}
else
{
   //not inserted
}

The create method returns the saved model instance

Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40
1

As per the docs

The create method returns the saved model instance:

In your case it would be

$user = User::create(...)
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
1
$data = User::create([
      'name' => $request->name,
      'email' => $request->email,
      'cell_phone' => $request->cell_phone,
      'province_id' => $request->province,
      'city_id' => $request->city,
      'job' => $job,
      'company' => $company,
      'member_num' => $member_num,
]);

After create, $data->id should be the last id inserted.

return Response::json(array('success' => true, 'last_insert_id' => $data->id), 200);
Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
K Ravi
  • 729
  • 8
  • 25
1

Here create and save both method returns last generated id but make sure you are make the field auto increment.

$userobj = User::create([
'name' => $request->name,
'email' => $request->email,
'cell_phone' => $request->cell_phone,
'province_id' => $request->province,
'city_id' => $request->city,
'job' => $job,
'company' => $company,
'member_num' => $member_num,
]);
echo $userobj->id;

if you want to use create method you have to define all the fields as a fillable in you model, and save() method used in both for create and edit so habit of using save method is good,

example of save method

 $user = User::first();
 $user->email = 'updated@domain.com';
 $user->save();
echo $user->id;

both method returns last inserted id. i hope it helps, Thank you...

Ritesh Khatri
  • 1,253
  • 13
  • 29