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