How to insert multiple rows from a single query using eloquent/fluent | Laravel Point

 

How to insert multiple rows from a single query using eloquent/fluent

In Laravel Eloquent, you can insert multiple rows into a database table using the insert method on the query builder. Here's an example:

$data = [    ['name' => 'John', 'email' => 'john@example.com'],
    ['name' => 'Jane', 'email' => 'jane@example.com'],
    ['name' => 'Jim', 'email' => 'jim@example.com'],
];

\DB::table('users')->insert($data);

 

This will insert three new rows into the users table, with the values specified in the $data array. Note that the \DB facade is used here to create an instance of the query builder, which can be used to interact with the database directly.

Previous Post Next Post

Contact Form