How to Insert Multiple Records in Database Laravel 10

How to Insert Multiple Records in Database Laravel 10

In this post we will give you information about How to Insert Multiple Records in Database Laravel 10. Hear we will give you detail about How to Insert Multiple Records in Database Laravel 10 And how to use it also give you demo for it if it is necessary.

In some scenarios, We need to insert multiple records into the database. In the laravel, You can insert multiple records into the database using Eloquent Model or Query Builder.

Suppose, one student can read many subjects and you want to store the information of a student who reads more than one subject and save it to a different table as student_subjects.

Use Laravel Eloquent Model or Query Builder to Insert Multiple Records in Database Laravel 10

In the example below, I use the multidimensional $createMultipleUsers array variable and insert multiple records using DB::insert(). So let’s see how to insert multiple records in laravel 10:

$createMultipleUsers = [
    ['name'=>'Admin','email'=>'[email protected]', 'password' => bcrypt('[email protected]')],
    ['name'=>'Guest','email'=>'[email protected]', 'password' => bcrypt('[email protected]')],
    ['name'=>'Account','email'=>'[email protected]', 'password' => bcrypt('[email protected]')]
];

User::insert($createMultipleUsers); // Eloquent
DB::table('users')->insert($createMultipleUsers); // Query Builder

Use Laravel Seeder to Insert Multiple Records in Database Laravel 10

You can generate dummy data using model Factories and faker to create fake data (with relations etc.) for developing and testing your app. Here you need to use the faker class for generation testing users. Let’s check out how we can use faker to generate fake records.

1. Create Laravel Seeder

php artisan make:seeder PostSeeder

2. Go to database/seeders and edit the PostSeeder.php file:

<?php

namespace DatabaseSeeders;

use IlluminateDatabaseSeeder;
use FakerGenerator as Faker;
use AppModelsPost;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create();

        foreach (range(1,20) as $index) {
            Post::create([
                'title' => $faker->text,
                'slug' => $faker->slug,
                'description' => $faker->text,
                'content' => $faker->content
            ]);
        }
    }
}

3. Use the db:seed command, Run the following command:

php artisan db:seed --class=PostSeeder

Thank you for reading this article.

Also see: How to read environment variables in Node.js

  .       .

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Hope this code and post will helped you for implement How to Insert Multiple Records in Database Laravel 10. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US