dimanche 20 mai 2018

Pivot Table Seeding

I have two models Status and StatusCategory which have a belongToMany relationship. I have a pivot table status_status_category with two fields status_id and status_category_id I have also made these two fields the primary key.

I have created model factories for my Status and StatusCategoryModel. When I ty to seed them I get the following exception in the terminal output:

Exception : Property [id] does not exist on this collection instance.

Relevant code is as follows:

    $factory->define(App\Status::class, function (Faker $faker) {
    return [
        'name' => $faker->word
    ];
});


$factory->define(App\StatusCategory::class, function (Faker $faker) {
    return [
        'name' => $faker->word
    ];
});

public function statuses() {
        return $this->belongsToMany( Status::class );
    }

public function statusCategories() {
        return $this->belongsToMany( StatusCategory::class );
    }

factory( Status::class, 30 )->create()->each( function ( $u ) {
            $u->statusCategories()->save( factory( StatusCategory::class )->make() );
        } );

I'm not sure where to turn here to be honest, if anyone can help? Thanks



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IzH71X
via IFTTT

Aucun commentaire:

Enregistrer un commentaire