Problems with Laravel Many to Many Polymorphic Relationships

♀尐吖头ヾ 提交于 2019-12-11 09:38:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!