jeudi 27 décembre 2018

Creation of multiple instance of an object

I am currently learning laravel and stuck with an assignment. I have the program where User will have multiple Whiteboards where he can post his notes. What i want to achieve is when User is created it should create 4 different Whiteboards.

This is working for 1 Whiteboard creation but not sure how I can achieve 4 while creating user. I have relationship set up already for single one.

protected function create(array $data)
{
    $whiteboard = Whiteboard::create([
        'username' => $data['username'],
        'name'     => $data['name'],
    ]);
    return User::create([
        'email'       => $data['email'],
        'password'    => bcrypt($data['password']),
        'whiteboard_id' => $whiteboard->id,
    ]);
}

I expect something like this (Just dummy example) -

User A should have whiteboards created with ID 1,2,3,4
User B should have whiteboards created with ID 5,6,7,8



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2EULKWe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire