Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel factory relationship one to many

// parent factory 

    public function configure()
    {
        return $this->afterCreating(function (Category $category) {
            $posts = Post::factory(50)->make();
            $category->posts()->saveMany($posts);
        });
    }

// call in seeder
Category::factory(10)->create();
 
PREVIOUS NEXT
Tagged: #laravel #factory #relationship
ADD COMMENT
Topic
Name
3+4 =