How To Use Factory in Seeder Laravel 8 Tutorial

Inside this article we will see the concept of Factory in seeder of laravel 8 tutorial. Using factory with faker library to seed fake data into database table.

This tutorial will be super easy to understand and easy to implement in your code. This tutorial will guide you to understand How to use factory in seeder laravel 8 tutorial. Factory is the feature of laravel artisan.

Learn More –

Let’s get started.

Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

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 Model & Migration

Open project into terminal and run this command to create model & migration file.

$ php artisan make:model Post -m

It will create two files –

  • Model — Post.php at /app/Models folder
  • Migration file — 2021_07_25_123805_2021_08_10_120110_create_posts_table at /database/migrations folder.

Open Migration file and write this complete code into it.

Learn More[…]