Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

juzza's avatar

Best way to create/update multiple database tables?

Hi all

Started learning Laravel a couple of weeks ago and now starting to write my first application as really this is the best way to learn so forgive me if my attempts are naive.

What is the best way to create/update one of more tables as part as my user registration?

I have two tables, one called 'members' and the other 'profiles'. I want to store the users email address and password in the 'members' table and then their real name etc. in the 'profiles' table. I need the members id to be stored in the profiles table.

This is how I am doing it currently.

    $memberinputs = $request->only('email', 'password');

    $user = Members::create($memberinputs);
    $member_id = $user->id;

This is storing the data as I wanted and then I am getting the ID for that stored record so I can use it in the 'profiles' table.

What I would like to do is add the $member_id into the form data so I create the record once. Problem is I don't know what is the best way to do this.

This is the way I am doing it and it is working but as you will see I have to interact with the database more than once which is not good :/

    $profileinputs = $request->only('role_player', 'role_referee', 'role_coach', 'role_club', 'language');

    $profile = Profile::create($profileinputs);
    $profile_id = $profile->id;

    $user = Profile::find($profile_id);
    $user->member_id = $member_id;
    $user->save();

As you can see I am creating the record to store the data captured from the form but then I am having to read the record back in to update it with the $member_id.

Is there a better, more efficient, way of doing this?

0 likes
6 replies
martinbean's avatar

As long as your relations are set up properly, you could insert the profile directly from the member model instance like this:

$member = Member::create($request->all());
$member->profile()->create($request->all());

If you need to touch multiple tables like this, then I’d suggest wrapping it in a service object that you pass the request data into, and it then does the data handling.

2 likes
juzza's avatar

Hi Martin

Thanks for the response and I have no doubt what you said is correct but unfortunately for me that goes way over my head. I thought I had done well to get this far but I don't know what you mean by relations being set up correctly or what a service object is to even know if I should be using one :/.

I think perhaps the problem is I am still learning and your solution is too advanced for me at this stage.

I don't want to waste your, or anyone else's time, babysitting me through this so I'll go back to the drawing board and see if I can learn so more.

1 like
j3rik0's avatar

https://laracasts.com/series/laravel-5-fundamentals/episodes/14 - Eloquent Relationships https://laracasts.com/series/laravel-5-fundamentals/episodes/23 - Syncing Tags

I think these two lessons should help you a bit with how to sync the data between tables and relationships.

Personally, I suggest you work along with Jeffrey in the https://laracasts.com/series/laravel-5-fundamentals/ series and stop when you see an example of something you want to do in your application, implement it and move along. At the end of the series you should have some pieces that should be easier to glue up in the final app.

ZeroGodForce's avatar

@martinbean Thanks for your solution, I had the exact same requirement and those two lines were just what I needed!

1 like

Please or to participate in this conversation.