Summary
I am creating a music search service. My question is: How do I insert data into a three-position pivot table Tag_Track_User ?
Scheme
I have this diagram visible here in LaravelSD
It consists of six main tables (and several others): Artists, albums, tracks, tags, users, and Track_User tag

Relationships Artists-> Albums-> Tracks are simple and, as you expected.
Tags, tracks and users are all connected to each other like no two can exist without a third .
Relations
Artists hasMany () Albums
Albums hasMany () Tracks and Owned To () Artist
Tracks owned by To () Albums
Tracks owned by ToMany () Tags and owned by ToMany () Users
Tags owned by ToMany () Tracks and owned by ToMany () Users
Users Own ToMany () Tags and Own ToMany () Tracks
Models
User model
public function tags() { return $this->belongsToMany('Tag', 'tag_track_user', 'user_mdbid', 'tag_mdbid')->withPivot('track_mdbid'); } public function tracks() { return $this->belongsToMany('Track', 'tag_track_user', 'user_mdbid', 'track_mdbid')->withPivot('tag_mdbid'); }
The Tag and Track model contains the same corresponding relationship.
Question
So my question is:
How to insert data into table Tag_Track_User? The tag_track_user table is a three-position pivot table that combines information about the tracks that users have marked.
You must be logged in to mark the track (this means that I have access to the user ID). The track ID opens as I display it on the page where the form is contained. Tag on the other hand; if it already exists in the tag table, I want to get its identifier and reuse it (since they are unique), if not, I want to create it, assign it an identifier and insert it into tag_track_user_table.
- I need to check if the tag exists
- If so, get the id
- Paste the data into the Tag_Track_User table
thanks
Any help I get on this is greatly appreciated.