Laravel, inserting into a three-way pivot table

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

Schema

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'); } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ 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.

+6
source share
2 answers

Well:

 $tag = Tag::firstOrCreate(array('text' => $tag_text)); TagTrackUser::create(array( "tag_mdbid" => $tag->mdbid, "track_mdbid" => $track->mdbid, "user_mdbid" => Auth::user()->mdbid )); 

Something like that? firstOrCreate does what the name says it does, the rest is pretty simple Eloquent.

+1
source

Since it seems that there is no corresponding template in Laravel, a simpler and more understandable way is to implement any three-turn relations using the model dedicated to the pivot table:

 class Track public function trackTags() { return $this->hasMany('TagTrack'); } ... class Tag public function tagTracks() { return $this->hasMany('TagTrack'); } ... class TagTrack public function track() { return $this->belongsTo('Track'); } public function tag() { return $this->belongsTo('Tag'); } public function anotherRelationship(){ ... } 

You can do:

 $track->trackTags->myCustomPivotDataAndRelationship() 

and in TagTrack you have the freedom to add as many relationships and fields that I want

Please note that you can still use many for many, when you do not need to refer to related relationships

0
source

All Articles