lundi 28 janvier 2019

How to create a model factory with foreign key 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())
    ];
});

Here here sbj_type_id relates to sbj_type table's primary key, but in sbj_type table it also relates to subject table because I have 5 subjects and several subjects types which relates to subjects but when I run faker it also fetches other subject types, i need a change here:

'sbj_type_id' => $faker->randomElement(App\SbjType::pluck('id')->toArray())



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire