we can get last insert id in three way to into laravel 5.7 application. laravel provide three model function to get current saved record id as save(), create() and insertGetId().
If you have experience with PHP Project developing then you know we almost require to get last id. so if you need to get last insert id then you can get save(), create() and insertGetId() method in laravel 5.7 project. here i will give you three way to get last inserted id or object in laravel 5.7 project.
So you can see following craeteNewUser() method with following three way to get and return last inserted id. you can use any one that is fit for your example. So let's see bellow examples:
Example 1: Using save()
public function craeteNewUser(){
$user_data =new User();
$user_data->name = "Akash Savani";
$user_data->email = "akash@gmail.com";
$user_data->save();
return $user->id;
}
Example 2: Using create()
public function craeteNewUser(){
$input = ['name' => 'Akash Savani', 'email'=>'akash@gmail.com'];
$user = User::create($input);
return $user->id;
}
Example 3: Using insertGetId()
public function craeteNewUser(){
$id = DB::table('users')
->insertGetId(
['name' => 'Akash Savani', 'email'=>'akash@gmail.com']
);
return $id;
}
I hope you found your best solution....
Do you like below Tutorials ?
- Angular Json Pipe Example | Json Pipe in Angular 9/8/7
- Angular Slice Pipe Example | Slice Pipe in Angular 9/8/7
- Angular Uppercase Pipe Example | Uppercase Pipe in Angular 9/8/7
- Angular LowerCase Pipe Example | LowerCase Pipe in Angular 9/8/7
- Angular Titlecase Pipe Example | Titlecase Pipe in Angular 9/8/7
- Angular KeyValue Pipe Example | KeyValue Pipe in Angular 9/8/7
- Call Method from One Component to Another Component in Angular
- Angular Push Object into Array Example