Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

viper75x's avatar

Factory with a foreign key

I'm using Ver 8, and creating factories and seeders to test the DB and code.

public function definition()
{
    return [
        'user_id' => '',
        'title' => $this->faker->sentence,
        'excerpt'=> $this->faker->sentence,
        'body' => $this->faker->paragraph
    ];
}

user_id is the foreign key, how can I get this factory to pull/reference the User id that already exists in the users table?

Thanks

0 likes
8 replies
SilenceBringer's avatar

Hi @viper75x usually it's done on the factory creation process

Model::factory()->create([
    'user_id' => $yourExistingUserId,
]);

(replace Model with your model name)

and in your definition you can just use factory for relation

public function definition()
{
    return [
        'user_id' => User::factory(),
        'title' => $this->faker->sentence,
        'excerpt'=> $this->faker->sentence,
        'body' => $this->faker->paragraph
    ];
}
viper75x's avatar

The first example you give, is the creation of the User model, correct?

Then, I can reference the factory() method of User class in...oh, I forgot to include that the example I gave is in another table.

But, if I reference the User::factory() in a related table, will that pull in the user id that already exists in the database?

SilenceBringer's avatar

@viper75x which model you define in your question? It was example for this model

ReplaceMeWithModelFromYourQuestion::factory()->create([
    'user_id' => $yourExistingUserId,
]);

and in this example I set existing user id

Official docs has a lot of information about factories, please pay attention

viper75x's avatar
viper75x
OP
Best Answer
Level 1

I did not find anything in the Docs that told me how I could create a post/article (code in original post), that would allow me to grab an existing user id and use that as the foreign key. I did find, in a video, this code:

'user_id' => \App\Models\User::inRandomOrder()->first()->id

This does grab an existing user id, rather than creating a new user alongside the post/article.

In the seeder file, I run

Article::factory(5)->create();

This is the code in the ArticleFactory file:

public function definition() { return [ 'user_id' => \App\Models\User::inRandomOrder()->first()->id, // 'user_id' => User::factory(), // creates new, non-existent user_id's 'title' => $this->faker->sentence, 'excerpt'=> $this->faker->sentence, 'body' => $this->faker->paragraph ]; }

2 likes
viper75x's avatar

By the way, how do I format the code to look like code? I can't seem to figure out how to do that again.

viktort1t0's avatar

You can hardcode the user id or, you can make the factory select one of the current users. something like this: 'user_id' => $this->faker->numberBetween(1, User::all()->count()),

clivew's avatar

I appreciate time (and Laravel) has moved on since the original question was asked, but I thought it might be useful to share what I've used recently in Laravel 11. Essentially, from a collection of id values from all of the User model instances, I've used Faker's randomElement() method to select one as a foreign key in a factory.

'user_id' => fake()->randomElement(User::pluck('id')), 
1 like

Please or to participate in this conversation.