我正在尝试设置 laravel-tagging ,它似乎最受欢迎Laravel的标签系统.但不幸的是,它没有任何前端功能.有一个指南为此,我彻底进行了跟踪.最后,尝试创建标签时遇到错误:
I am trying to set up laravel-tagging, which seems to be the most popular tagging system for Laravel out there. But unfortunately it doesn't come with any front-end features. There is a guide for it that I followed through thoroughly. At the end, I run into error while trying to create a tag:
SQLSTATE [23000]:违反完整性约束:1048列 'taggable_id'不能为null(SQL:插入tagging_tagged (tag_name,tag_slug,taggable_type,taggable_id)值 (奶酪,奶酪,App \链接,))
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'taggable_id' cannot be null (SQL: insert into tagging_tagged (tag_name, tag_slug, taggable_type, taggable_id) values (Cheese, cheese, App\Links, ))
我发现了其他一些帖子,人们遇到了类似的错误,例如 this ,此和此.但是它们都不提供确定的解决方案.人们常识说,应该保存包含taggable_id的模型,以便将标记存储在数据库中.我为控制器编写的代码如下:
I found several other posts where people ran into similar errors, such as this, this and this. But none of them provide a definitive solution. People, and the common sense, say that the model that contains taggable_id should be saved so the tag gets stored in the database. My code for the controller looks like this:
public function storeStuff(Request $request) { // Create the link first $link = new Links; // Now add tags $link->tag(explode(',', $request->tags)); // Try to save tags? $link->save(); }在我的情况下,我尝试使用$link->save();保存它的尝试无效.我仍然遇到相同的错误,并且包含taggable_id列的数据库表tagging_tagged仍然充满空值.有人对如何解决此问题有任何建议吗?
My attempt to save it using $link->save(); in my case doesn't work. I still get the same error and my database table tagging_tagged, which contains taggable_id column is still full of nulls. Does anyone have any advice on how to approach this problem?
编辑:我通过添加另一个保存来工作,如Tobias Karlsson建议的那样:
I got it to work by adding another save as suggested by Tobias Karlsson:
$link = new Links; $link->tag_name = $request->tags; $link->save(); // Now add tags $link->tag(explode(',', $request->tags)); $link->save();我还必须添加时间戳以修复丢失的created_at错误.时间戳不包含在laravel-tagging软件包随附的初始迁移中,即使我不确定它们是否对标签有用.我的tagging_tagged表现在看起来像这样:
I also had to add timestamps to fix missing created_at error. Timestamps are not included in initial migration that comes with laravel-tagging package, even though I am not sure if they are useful for tags. My tagging_tagged table now looks like this:
FIELD TYPE NULL KEY id int(10)unsigned NO PRI auto_increment taggable_id int(10)unsigned NO MUL taggable_type varchar(255) NO MUL tag_name varchar(255) NO tag_slug varchar(255) NO MUL created_at timestamp YES updated_at timestamp YES我仍在尝试使用Javascript自动填充标签.一切正常后,我将更新此问题.
I am still trying to get Javascript to autofill tags. I will update this question once I get it all working.
推荐答案您将必须先保存模型,然后设置标签,然后再次保存.
You will have to save the model first and then set tags and then save again.
$link = new Links; $link->someProperty = $request->someProperty; // Save model to get a taggable_id (model id). $link->save(); // Now add tags $link->tag(explode(',', $request->tags)); // Save tags. $link->save();更多推荐
违反完整性约束:1048列"taggable
发布评论