我正在使用select2选择帖子中的标签,但我想实现标签功能。我能够保存新标签,但仍无法将标签链接到数据透视表上的帖子。
SQLSTATE [HY000]:一般错误:1366不正确的整数值:“自行车” 对于第1行中的“ tag_id”列(SQL:插入
post_tag
(post_id
中,tag_id
)值(167,自行车))
$post = new Post;
$post->post_category_id = $request->post_category_id;
$post->title = $request->title;
$post->body = $request->body;
$post->meta_description = $request->meta_description;
$post->slug = $request->slug;
$post->user_id = auth()->id();
$tags = $request->input('tags');
foreach ($tags as $tag) {
if (is_numeric($tag)) {
$tagArr[] = $tag;
} else {
$newTag = Tag::create(['name' => $tag]);
$newTag = $newTag->id;
}
}
$post->save();
$post->featured()->sync($request->featured, false);
$post->tags()->sync($request->tags, false);
Session::flash('success', 'The blog post was saved successfully!');
return redirect()->route('posts.show', $post->id);
我知道我可以将tag_id
更改为字符串,但是我想知道是否还有另一种方法。