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]: 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, AppLinks, ))
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();
}
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?
EDIT: 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();
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
I am still trying to get Javascript to autofill tags. I will update this question once I get it all working.
See Question&Answers more detail:
os