0

I'm using laravel factories to generate some fake users in my database but I don't know how to get the user id of the user that I'm currently generating. I want to get the id of the current user so I can hash it and put it in the slug.

This is my code so far:

$factory->define(User::class, function (Faker $faker) {
$name = $faker->name;
$email = $faker->unique()->safeEmail;
$date_of_birth = $faker->date();

return [
    'name' => $name,
    'email' => $email,
    'email_verified_at' => now(),
    'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
    'remember_token' => Str::random(10),
    'date_of_birth' => $date_of_birth,
    'slug' => (\App\User::class)->id //This is the part that doesn't work,
];
});
Max
  • 357
  • 1
  • 6
  • 16

3 Answers3

2

Another way to approach it would be to set the slug outside of the factory.

This would mean that slug would need to be nullable though.

Migration:

$table->string('slug')->nullable();

Factory:

$factory->define(User::class, function (Faker $faker) {
$user = [
    'name' => $faker->name,
    'email' => $faker->unique()->safeEmail,
    'email_verified_at' => now(),
    'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
    'remember_token' => Str::random(10),
];

return $user;
});

Seeder:

$users = factory(User::class, 5)->create();

foreach($users as $user) {
    $user->slug = $user->id;
    $user->save();
}

UPDATE:

Laravel offers Factory Callbacks (See Docs)

So, you don't need to loop through in your seeder, just chain the afterCreating() method:

$factory->define(App\User::class, function (Faker $faker) {
return [
    'name' => $faker->name,
    'email' => $faker->unique()->safeEmail,
    'email_verified_at' => now(),
    'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
    'remember_token' => Str::random(10),
];
})->afterCreating(\App\User::class, function (\App\User $user, Faker $faker) {
    $user->slug = $user->id;
    $user->save();
});
kerrin
  • 3,376
  • 1
  • 26
  • 36
0

If your users.id is auto increment, you can get the same ID if you use the answer of the question I gave in comment, by using static variable and increment it.

$factory->define(App\AliasCommand::class, function (Faker\Generator $faker) {
    $name = $faker->name;
    $email = $faker->unique()->safeEmail;
    $date_of_birth = $faker->date();

    static $id = 1;

    return [
        'name' => $name,
        'email' => $email,
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
        'date_of_birth' => $date_of_birth,
        'slug' => $id++,
    ];
});

Not tested, but this is how I did it a while ago.

Christhofer Natalius
  • 2,727
  • 2
  • 30
  • 39
0

ID is only assigned after the record is inserted into the database. So there is no way to access it in your factory.

Assuming you need the slug not only as part of your testing/seeding, the best way is to hook into the model's created event:

class User extends Authenticatable
{
    protected static function boot() {
        parent::boot();

        static::created(function ($user) {
            $user->update(['slug' => $user->id]);
        });
    }
}
Maksim Ivanov
  • 3,991
  • 31
  • 25