Loading…

What are the difference between insert() and insertGetId() in laravel?

Inserts(): This method is used for insert records into the database table. No need the “id” should be autoincremented or not in the table.

Example

DB::table('bestinterviewquestion_users')->insert(
    ['title' => 'Best Interview Questions', 'email' => ‘info@bestinterviewquestion.com’]
);

It will return true or false.

 

insertGetId(): This method is also used for insert records into the database table. This method is used in the case when an id field of the table is auto incrementing.

It returns the id of current inserted records.

Example

$id = DB::table('bestinterviewquestion_users')->insert(
    ['title' => 'Best Interview Questions', 'email' => ‘info@bestinterviewquestion.com’]
);

Suggest An Answer

Our Particitants

Latest post from Laravel Interview Questions

Ask Question