How to insert multiple records in table using laravel?

Member

by aubrey , in category: PHP Frameworks , a year ago

How to insert multiple records in table using laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , a year ago

@aubrey 

To insert multiple records in a table using Laravel, you can use the insert method provided by Eloquent. Here is an example code snippet to demonstrate how to insert multiple records:

1
2
3
4
5
6
7
$data = [
    ['name' => 'John Doe', 'email' => 'john@example.com'],
    ['name' => 'Jane Smith', 'email' => 'jane@example.com'],
    ['name' => 'Bob Johnson', 'email' => 'bob@example.com'],
];

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


In this example, we have an array of data where each element represents a record to be inserted. We then call the insert method on the users table in the database and pass the array of data as an argument.


This will insert multiple records into the users table in a single query, which is more efficient than inserting records one by one.

Related Threads:

How to save multiple table rows in database using laravel?
How to update records in a table using phpMyAdmin?
How to delete records from a table using phpMyAdmin?
How to insert data into a table using phpMyAdmin?
How to insert multiple emails in laravel?
How to insert multiple rows in laravel?