问题
Really I need exactly what is in the Eloquent documentation. I need a tag system, with a polymorphic many to many relationship with users, posts, etc. The documentation doesn't seem to clearly state how to insert or update records with this relationship though... my attempts to do things like this:
$user->tags()->save($tag);
or
$tag->users()->attach(Input::get('userID'));
(which the above results in this error)
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tag_id' cannot be null (SQL: insert into `taggables` (`tag_id`, `taggable_id`, `taggable_type`) values (, 1, User))","file":"\/Users\/austinslominski\/Sites\/Mumble\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":625}}
My database is constructed in the way that is shown in the documentation:
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
Is the tag_id supposed to be auto-incrementing? How have some of you dealt with this?
来源:https://stackoverflow.com/questions/27029277/problems-with-laravel-many-to-many-polymorphic-relationships