DEV Community

Cover image for Laravel Seeders and Factories
Chetan Rohilla
Chetan Rohilla

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

15 4 1 1 1

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:)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay