Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel Database Seeder using insert multiple records

Laravel Database Seeder using insert multiple records

Posted on August 6, 2019December 17, 2022 By XpertPhp 2 Comments on Laravel Database Seeder using insert multiple records

Today, we will tell you about the Laravel 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.

Install Laravel

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
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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
1
php artisan migrate
See also  Laravel 7 Pagination Example Tutorial

Step3: Create the Model.

we create a student model using the below command.

PHP
1
php artisan make:model Student

Step4: Create the seeder class

we will create StudentsTableSeeder class using the below command.

PHP
1
php artisan make:seed StudentsTableSeeder

Now, open the file StudentsTableSeeder.php at the database/seeds directory and replace the below content.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
    use Illuminate\Database\Seeder;
    use App\Student;
 
    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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
    use Illuminate\Database\Seeder;
 
    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
1
php artisan db:seed

Laravel, MySql Tags:Laravel Database Seeder, laravel from scratch

Post navigation

Previous Post: Login With Facebook Using CodeIgniter
Next Post: How to get or select last row from the Table in laravel

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com