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

Ligonsker's avatar

Updating relationship tables

If I set up 3 tables in the db: users, posts and the relationship table user_post

And I set the correct relationships. What is a correct way to update the correct tables when I update a table?

For exmaple, if I create a new post:

$post = Post::create([
    'user_id' => $user_id,
    'data' => $data
]);

In this case I will need to also update the user_post table with the correct user_id and post_id.

What is the Laravel way of doing so instead of doing 2 queries?

0 likes
14 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The signed in user?

Auth::user()->posts()->attach($post);

But why would posts have a user_id if there can be multiple users ?

1 like
Ligonsker's avatar

@Sinnbeck I mean that a user created this post. By multiple users, you mean that they can view it? Or edit it?

Sinnbeck's avatar

@Ligonsker Totally up to you. I have seen people do stuff like that :) LIke an author and editors. Or profile image and other images

1 like
Ligonsker's avatar

@Sinnbeck This was just an example to be honest, in reality I want to do it with folders, images and folder_image to know to which folder the image belongs to - in this case it makes more sense?

So if I want to allow users to organize their images in different folders I want a relationship between the folder and the image

But also, I even made a direct user_image relationship (for the original image uploader), So:

  • image belongs to user and belongs to folder
  • folder belongs to user and has many images
  • user has many images and has many folders

Is it ok or you would change something?

Sinnbeck's avatar

@Ligonsker I would probably just add a many to many between users and images, and have the folder be part of the image name :) Each image only has 1 folder anyways

Ligonsker's avatar

@Sinnbeck By folder I mean a "virtual" folder - not the actual path. The actual path is a random and permanent path set when using the $file->store() method,

The reason is I want to allow users to create/delete folders in their gallery. So these are virtual folders and what changes is their relationship in the database.

So if a user decides to create a "TripA" folder, he can then move the files there from the "Default" folder, but what actually happens is that I just update the folder_id in the folder_image for example

Or it's not a good way to do that?

Sinnbeck's avatar

@Ligonsker I cannot think of an exact scenario where this would be needed, but if it makes sense it your app, then sure go for it :) I assume you are building some sort of file manager?

1 like
Ligonsker's avatar

@Sinnbeck Btw, when you said "I cannot think of an exact scenario where this would be needed" - did you mean that the virtual folders themselves aren't needed(because that was before you knew it's for a file manager and then it's OK)?

Or you meant that even for a file manager it's not the way to do it?

Sinnbeck's avatar

@Ligonsker Well I mostly changed my mind halfway through the sentence as it occurred to me that it might be a file manager.

1 like
Ligonsker's avatar

@sinnbeck BTW, Regarding the attach() - I still need to do it in 2 actions right?

$post = Post::create([
    'user_id' => $user_id,
    'data' => $data
]);

Auth::user()->posts()->attach($post);

There isn't a magic Laravel "do it together" method?

Please or to participate in this conversation.