Laravel 8 Database Seeder with Sample Data Insertion

Share this Article
Reading Time: 3 minutes
4,006 Views

Inside this article we will see the concept of database seeding with sample data in laravel 8. Laravel 8 database seeding from example data is a technique to dump test data into tables in bulk.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. Database seeding is the process in which we feed test data to tables. We can insert data either using Faker library, manual data or means of some more data sources like CSV, JSON.

Laravel 8 Database Seeding from JSON File Tutorial, Click here.

In this tutorial we will use the concept of Laravel 8 Database Seeder with Sample data insertion.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Create Database Seeder

Suppose we have a students table in application database.

We will create a database seeder for that table and also will insert example data into it.

Open project into terminal and run this artisan command.

$ php artisan make:seeder StudentsTableDataSeeder

It will create a file class StudentsTableDataSeeder.php inside /database/seeders folder.


Seeder with Example Data

Open StudentsTableDataSeeder.php and write this complete code into it.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class StudentsTableDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        for ($i = 0; $i < 10; $i++) {

            DB::table('students')->insert([
                'name' => Str::random(8),
                'email' => Str::random(12) . '@gmail.com',
                'password' => bcrypt('123456')
            ]);
        }
    }
}

Next, we need to run this Student seeder to seed example data into students table.

Run Seeder

Back to project terminal.

$ php artisan db:seed --class=StudentsTableDataSeeder

This command will seed 10 rows of example data into students table.

We hope this article helped you to learn Laravel 8 Database Seeder with Sample Data Insertion Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4