How to Insert Data in Table Using Laravel Query builder?

If you have an array of records then you can add simple by using insert() method. This is very simple while we use Laravel query builder. Here is an example.

$leads = [
	[ 'name' => 'Jason Roy', 'email' => '[email protected]' ],
	[ 'name' => 'Sabrina Arther', 'email' => '[email protected]' ]

DB::table( 'leads' )->insert( $leads );

If you want to insert only one record and you want to use its insertID in anther query later then you can use insertGetID() method. This will return ID of newly inserted record.

$lead = [ 'name' => 'Emily Blunt', 'email' => '[email protected]' ];
$id = DB::table( 'leads' )->insertGetId( $lead );

Now I hope you understand everything about insert() method in Laravel Query builder. If you have any problem please leave a comment. We appreciate that.