DEV Community

Cover image for Laravel Seeders and Factories
Chetan Rohilla
Chetan Rohilla

Posted on • Updated on • Originally published at w3courses.org

Laravel Seeders and Factories

Here, i have come with some code to create laravel seeders and factories. For inserting data in our database for testing purpose.

Create Model Admin

php artisan make:model Admin -m
Enter fullscreen mode Exit fullscreen mode

Now, open the admin migration file in database directory and table columns.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAdminsTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create( 'admins', function (Blueprint $table)
        {
            $table->increments( 'id' );
            $table->string( 'name' );
            $table->string( 'surname' );
            $table->string( 'email' );
            $table->string( 'password' );
            $table->timestamps();
        } );
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists( 'admins' );
    }

}
Enter fullscreen mode Exit fullscreen mode

To create table run

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Create Factory in Laravel to create random number of records for testing. We will create factory AdminFactory.

php artisan make:factory AdminFactory
Enter fullscreen mode Exit fullscreen mode

Inside the definition method set your database table columns.

<?php

namespace Database\Factories;

use App\Models\Admin;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class AdminFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Admin::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'surname' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'password' => bcrypt('123456'), // password
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, create seeder to run factory or database queries.

php artisan make:seeder AdminSeeder
Enter fullscreen mode Exit fullscreen mode

Inside run method apply factory on Admin model.

namespace Database\Seeders;

use App\Models\Admin;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class AdminsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Admin::factory()
            ->count(50)
            ->create([
                'surname' => 'Kumar',
            ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now run the seeder class

php artisan db:seed --class=AdminSeeder
Enter fullscreen mode Exit fullscreen mode

This will create 50 random records in table admins


Please like share and give positive feedback to motivate me to write more.

For more tutorials visit my website.

Thanks:)
Happy Coding:)

Top comments (0)