samedi 27 octobre 2018

Laravel Factories - How to save nested relationships

I was working with laravel factories and stuck with nested relation ships. For example a user may have many posts and a post has many comments. i was able to save posts with user by each() method but was not able to save comments with posts

$users = factory(App\User::class, 3)->create()
    ->each(function ($user) {
        $user->posts()->saveMany(factory(App\Post::class, 5)->make());
    });

as save() method accepts array so i must have to use make() method with posts but now i can't attach comments with the posts. after hour of searching i could not fond the solution but now i have solved it. I am posting it in answers for the fellows who are looking for the solution and please update me if there have more decent solution to this. Thanks



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

Aucun commentaire:

Enregistrer un commentaire