如何使用外键laravel创建模型工厂

I have a model factory as:

QuestionFactory.php:

$factory->define(App\Question::class, function (Faker $faker) {
    return [
        'question' => $faker->text($maxNbChars = 150),
        'subject_id' => $faker->randomElement(App\Subject::pluck('id')->toArray()),
        'sbj_type_id' => $faker->randomElement(App\SbjType::pluck('id')->toArray())
    ];
});

but the above code also fetches the other subject type heresbj_type_id is linked to sbj_type table and then there's also a field that which a sbj_type relates, .e.g., I have 2 subjects and 4 sub type so I have 2 type linked to 1 subject and other 2 relates to second subject so I need to fetch according to subject_id also, I have done this

'sbj_type_id' => $faker->randomElement(App\SbjType::where('subject_id', App\Subject::inRandomOrder()->value('id'))->pluck('id')->toArray())

All I need to fetch sbj_type according to subject_id

Use the query builder's inRandomOrder paired with the value method to get a random ID directly out of the database:

App\Subject::inRandomOrder()->value('id')

To put that into your factory, it'd look like this:

$factory->define(App\Question::class, function (Faker $faker) {
    return [
        'question' => $faker->text($maxNbChars = 150),
        'subject_id' => App\Subject::inRandomOrder()->value('id'),
        'sbj_type_id' => App\SbjType::inRandomOrder()->value('id'),
    ];
});

I have solved my problem, I used:

$subject = App\Subject::inRandomOrder()->value('id');
return [
    'question' => $faker->text($maxNbChars = 150),
    'subject_id' => $subject,
    'sbj_type_id' => App\SbjType::where('subject_id', $subject)->inRandomOrder()->value('id'),
]

First of all I get subject_id from database and stored it into a variable $subject after that in faker code I used that variable in the condition so that's it I solved my problem, Thanks to @JosephSilber