我有这些:
员额表
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title', 64);
$table->string('teaser', 128)->nullable();
$table->text('content', 50000);
$table->timestamps();
});
}
岗位模型
use HasFactory;
protected $fillable = ['title', 'teaser', 'content'];
public function tags()
{
return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id');
}
标签表
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('text', 32);
});
}
标签模型
use HasFactory;
public $timestamps = false;
public $fillable = ['text'];
public function posts()
{
return $this->belongsToMany(Post::class, 'post_tag', 'tag_id', 'post_id');
}
post_tag表
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('post_id');
$table->unsignedInteger('tag_id');
});
}
当我尝试创建一个带有标签的新帖子时,我得到这个错误:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'test' for column `laravel`.`post_tag`.`tag_id` at row 1
INSERT INTO
`post_tag` (`post_id`, `tag_id`)
VALUES
(31, test)
我是这么做的:
public function store(PostFormValidation $request)
{
$newpost = Post::create($request->validated());
$newpost->tags()->sync($request->tags);
return redirect(route('home'));
}
但是当我把时间戳从迁移中移除并指定我不在模型中使用它们时,为什么它会抱怨时间戳呢?我错过了什么?
提交的“标签”是一个多选。
2条答案
按热度按时间f4t66c6m1#
您在字段tag_id中键入了"测试“字,但tag_id为
unsignedbiginteger
6kkfgxo02#
我认为你的错误在于:
我建议查看此laravel文档,以查看格式应该是:
或者: