SQL Many to Many relationships

Are there any eloquent solutions to handle many to many relationships in forms?

Example: Post has many Tags and a Tag can belong to many posts.

My thought is, there must be a way to use a multi-select in a form to accomplish this.

@aaronmorse Can you post an example (screenshots) of what you're trying to build?

@ScottR Yes.

Here's an example.

There are three table in a SQL database.

  1. Questions
  2. Answers
  3. Question Answers (many-to-many)

My objective:

  1. Create a question in the Questions Table
  2. Link the selected answers via the Question Answers Table

Solutions:

  • Use an API endpoint

Possible Solution:

  • Run the queries in sequence. Starting with creating a question. Followed by creation the question answer records.

I think the last option is best. Keep it simple and build the base to you set the right relationships between tables.

I use the last one with a twist. I use a transaction within a stored procedure in my database so I don't end up with orphaned records if something goes wrong halfway through. Then I can also just use the one query in Retool keeping things cleaner and easier.

I pass the main table ID and the .value array from the multiselect ad let the SP do the dirty work. If you need an example I can send one along (I think I have a post here that goes into this more detail somewhere can't find it at the moment.)