mercredi 16 mars 2016

laravel model factory seed generate from existing

I am trying to seed a table with a foreign key and I'm stuck on how I would tell the model to randomly pull values from what already exists.

ModelFactory

$factory->define(App\Device::class, function(Faker\Generator $faker) {
    return [
        'vendor' => ,
        'name' => $faker->company,
        'mac_address' => $faker->macAddress,
    ];
});

I seed the Vendor table before the Device table and would like to populate a random vendor id for the vendor property. I have tried the following:

'vendor' => 'factory::App\Vendor'

But I am getting

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: foreign key constraint fails

It looks like the insert is trying to insert factory::App\Vendor for the vendor column. I am trying to figure out how to have it pull from the existing vendors.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire