In Laravel, you need to create a user factory seed using the built-in model factories provided by Laravel. Main use of these factories to generate database records for testing and seeding purposes in a very quick way.

By following the steps below, you can generate user factory seeds in Laravel, create factory files and seed your database with the user data. You will also learn to create factories and link them with seeders. Follow the below steps-

1. Create a User Factory

At first, you need to create a factory for the User model. In Laravel 8 or latest version, you can use the following command to create User model:

php artisan make:factory UserFactory

This will create a factory file at database/factories/UserFactory.php.

2. Define the User Factory

Open the database/factories/UserFactory.php file and establish your user structure. It could look something like this:

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name(),
            'email' => $this->faker->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => Hash::make('password'), // default password
            'remember_token' => Str::random(10),
        ];
    }
}

You can use Faker to generate random user data in this factory, such as name and email, as well as define a default password.

3. Create a Seeder for Users

The next step is to design a seeder that will contact the factory to generate users. To generate a seeder, run the following command:

php artisan make:seeder UsersTableSeeder

This will create a file at database/seeders/UsersTableSeeder.php.

4. Define the Seeder Logic

In the UsersTableSeeder.php file, use the factory to generate users:

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Generate 10 users using the factory
        User::factory()->count(10)->create();
    }
}

5. Run the Seeder

To seed your database with user data, first connect the seeder to the main DatabaseSeeder.php file, which may be found in the database/seeders directory.
 
In DatabaseSeeder.php, add the following line to the run method:
$this->call(UsersTableSeeder::class);

Now, run your seeders using the following command:

php artisan db:seed

This will execute the UsersTableSeeder and generate 10 user records in your users table.

Optional: Create Custom User 

You can also create specific users during seeding:

User::factory()->create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => Hash::make('password123'),
]);

This allows you to blend randomly generated user data with specified users, such as an administrator or a specific user profile.

6. Run Database Migrations and Seeding Together

To migrate your database and seed it in one step, run:

php artisan migrate --seed

This approach provides you with both random user data and custom users during database seeding.

Share.
Leave A Reply

Exit mobile version