##Summary##
I am building a music discovery service. My question is: How do I insert data into the three-way pivot table Tag_Track_User ?
##Schema##
I have this schema seen here at LaravelSD
It comprises of six main tables (and a few others): Artists, Albums, Tracks, Tags, Users and Tag_Track_User
The Artists->Albums->Tracks relationship is straightforward and as you'd expect.
Tags, Tracks and Users all relate to one-another as no two can exist without the third.
##Relationships##
Artists hasMany() Albums
Albums hasMany() Tracks and belongsTo() an Artist
Tracks belongsTo() Albums
Tracks belongsToMany() Tags and belongsToMany() an Users
Tags belongsToMany() Tracks and belongsToMany() an Users
Users belongsToMany() Tags and belongsToMany() an 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 contain the same respective relationships.
##Question## So my question is:
How do I insert data into the Tag_Track_User table? The tag_track_user table is a 3-way pivot table cointaining information about tracks that users have tagged.
You have to be logged in to tag a track (which means I have access to the user’s ID). The tracks ID is accessed as I am displaying it on the page where the form is contained. The tag on the other hand; if it already exists in the tags table, I want to get it’s ID and re-use that (as they are unique), if not, I want to create it, assign it an ID and insert that into the tag_track_user_table.
##Thank you##
Any help I receive on this, is greatly appreciated.
Hi mstnorris!
That is indeed a good question and one that laravel can handle with ease nowadays:
The main task (3.) you are looking for is specified in the Laravel docs under http://laravel.com/docs/eloquent#inserting-related-models (the last codeblock in that chapter).
But lets start with task 1, which can be combined with task 2 via the nice firstOrCreate()
method:
(http://laravel.com/docs/eloquent#insert-update-delete)
$tag_name = Input::get('tag_name'); // or however you called your input field
$tag = Tag::firstOrCreate( array('text' => $tag_name) );
This method searches for the tag and creates it if it doesn't exist.
So now that you have the tag and its ID through
$tag->mdbid
we can insert everything into the pivot table through the save-method:
// Find the track model
$track = Track::find( Input::get('track_mdbid') ); // or however you submit the track ID
// Insert the relationship via the logged-in user
Auth::user()->tracks()->save( $track, array('tag_mdbid' => $tag->mdbid) );
I am not sure, but this should also work with update() instead of save(). If not, you propably have to use the sync() method described here under the topic "Adding Pivot Data When Syncing".
That should do the trick ;-)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community