Back

how to get the user id in faker


while created faker how to get the user_id for the post?

for example: i've created a faker for User table.

public function run()
    {
        $faker = Factory::create();

//        User::truncate();


        foreach (range(1, 10) as $i) {
            User::create([
                'email' => $faker->email,
                'first_name' => $faker->name,
                'password' => bcrypt('admin123'),
            ]);
        }
    }

and next i made a faker for posts_table

public function run()
    {
        $faker = Factory::create();

        foreach (range(1, mt_rand(2, 6)) as $j) {
            Post::create([
                'body' => $faker->sentence,
                'user_id' => ?????
            ]);
        }
    }

how to i get the user id in faker? is it possible?

sbimochan replied 2 months ago
'user_id'=>$factory->create(\App\User::class)->id

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us