Laravel Database Seeder using insert multiple records – onlinecode
In this post we will give you information about Laravel Database Seeder using insert multiple records – onlinecode. Hear we will give you detail about Laravel Database Seeder using insert multiple records – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Today, we will tell you about the Database Seeder. Laravel is a PHP framework. it provides many facilities such as Database Seeder. let’s see that taking about the laravel Database Seeder.
Database seeder means if you want to insert multiple records or some dummy data in the database it is called database seeder.
Step 1: Install Laravel and setting the database configuration.
You can follow our below article URL for this step.
Step 2: Create Table using migration
Now, We need to create a migration. so we will below command using create the students table migration.
php artisan make:migration create_students_table --create=students
After complete migration. we need below changes in the database/migrations/create_students_table file.
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateStudentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('students', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('first_name'); $table->string('last_name'); $table->text('address'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } } ?>
Run the below command. after the changes above file.
php artisan migrate
Step3: Create the Model.
we create a student model using the below command.
php artisan make:model Student
Step4: Create the seeder class
we will create StudentsTableSeeder class using the below command.
php artisan make:seed StudentsTableSeeder
Now, open the file StudentsTableSeeder.php at the database/seeds directory and replace the below content.
<?php use IlluminateDatabaseSeeder; use AppStudent; class StudentsTableSeeder extends Seeder { public function run() { for ($i=0; $i < 6; $i++) { Student::create([ 'first_name' => str_random(10), 'last_name' => str_random(10), 'address' => str_random(25) ]); } } } ?>
When we complete the changes, then we have to need to edit the database/seeds/DatabaseSeeder.php
<?php use IlluminateDatabaseSeeder; class DatabaseSeeder extends Seeder { public function run() { $this->call([ UsersTableSeeder::class, StudentsTableSeeder::class, ]); } } ?>
Now, when we will execute the below command then DatabaseSeeder class is called and it will insert the records into the database.
php artisan db:seed
Hope this code and post will helped you for implement Laravel Database Seeder using insert multiple records – onlinecode. 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