Daniel-Pablo's avatar

create user with profile table

Hi, I am trying to create a user with a profile table related so far...

      $newUser = User::create([
            'name' => 'daniel',
            'email' => '[email protected]',
            'password' => Hash::make(123),
      ]);

      Profile::create([
            'user_id' => $newUser->id,
            'nombre' => 'Adminer',
            'apellido' => 'Jhonson',
      ]);

this is working but I thing there should be a better way to do it... can some one help me? am using for a role this one

$newUser->role()->attach(1);

I love this way

so I want to make something like this

$newUser->Profile()->attach($newUser->id); // this is my idea to create an attached table to that ID user

thanks in advance for your help

0 likes
2 replies
ellgreen's avatar
ellgreen
Best Answer
Level 5

If you had a relationship on the User model for Profile, you could do the following:

User::create([...])->profile()->create([...]);
Daniel-Pablo's avatar

thanks @ellgreen , I try this with your code up and get a positive result!!


      $newUser = User::create([
            'name' => 'daniel',
            'email' => '[email protected]',
            'password' => Hash::make(123),
      ]);

$newUser->profile()->create();
$newUser->financialProfile()->create();
$newUser->contracts()->create();

// ALL 3 CREATE THE TABLES WITH THE ID OF THE USER THANKS A LOT,	


Regards Daniel

1 like

Please or to participate in this conversation.