How Create Multiple Records in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn how to create multiple records in laravel.

 

How to Create Multiple Records in Laravel

To create multiple records in Laravel, you can use Eloquent's createMany() method, which allows you to create multiple records in a single query. Here's an example of how to use createMany():

Assuming we have a model called User, and we want to create multiple users with the following data:

$users = [  
    [   'name' => 'John Doe',        'email' => 'john@example.com',        'password' => 'password1'    ],
    [   'name' => 'Jane Doe',        'email' => 'jane@example.com',        'password' => 'password2'    ],
    [   'name' => 'Bob Smith',        'email' => 'bob@example.com',        'password' => 'password3'    ]
];

To create these users, we can use the createMany() method on the User model:

User::createMany($users);

This will create three user records in the database with the provided data.

Note that the createMany() method only works if the columns you want to insert are fillable in the model. If you want to insert columns that are not fillable, you'll need to use the insert() method instead.

 

How to insert multiple data using DB in Laravel

here's an example of how to use the insert() method to create multiple records in Laravel:

Assuming we have a table called users with the following columns: name, email, and password, and we want to create multiple users with the following data:

$users = [    
    [   'name' => 'John Doe',        'email' => 'john@example.com',        'password' => 'password1'    ],
    [   'name' => 'Jane Doe',        'email' => 'jane@example.com',        'password' => 'password2'    ],
    [   'name' => 'Bob Smith',        'email' => 'bob@example.com',        'password' => 'password3'    ]
];

To create these users, we can use the insert() method on the DB facade:

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

This will insert the three user records into the users table with the provided data.

Note that when using the insert() method, you'll need to manually set the timestamps if the table has timestamp columns. You can do this by adding the created_at and updated_at columns to each record:

$users = [    
    [  'name' => 'John Doe',        'email' => 'john@example.com',        'password' => 'password1',        'created_at' => now(),        'updated_at' => now()    ],
    [  'name' => 'Jane Doe',        'email' => 'jane@example.com',        'password' => 'password2',        'created_at' => now(),        'updated_at' => now()    ],
    [  'name' => 'Bob Smith',        'email' => 'bob@example.com',        'password' => 'password3',        'created_at' => now(),        'updated_at' => now()    ]
];

In this example, we've added the created_at and updated_at columns to each record with the current timestamp using the now() helper function. This will ensure that the timestamps are set correctly for each record.

Comments 0

Leave a comment