英文:
Why am I unable to NOT use timestamps in Laravel?
问题
我有这些内容:
posts表
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();
});
}
posts模型
use HasFactory;
protected $fillable = ['title', 'teaser', 'content'];
public function tags()
{
return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id');
}
tag表
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('text', 32);
});
}
tag模型
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'));
}
但是为什么它会抱怨时间戳,当我从迁移中删除了它们,并且在模型中指定我不使用任何时间戳呢?我漏掉了什么?提交的 "tags" 是一个多选框。
英文:
I have these:
posts table
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();
});
}
posts model
use HasFactory;
protected $fillable = ['title', 'teaser', 'content'];
public function tags()
{
return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id');
}
tag table
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('text', 32);
});
}
tag model
use HasFactory;
public $timestamps = false;
public $fillable = ['text'];
public function posts()
{
return $this->belongsToMany(Post::class, 'post_tag', 'tag_id', 'post_id');
}
post_tag table
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('post_id');
$table->unsignedInteger('tag_id');
});
}
When I try to create a new post with tags, I get this error:
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)
This is how I'm trying to do it:
public function store(PostFormValidation $request)
{
$newpost = Post::create($request->validated());
$newpost->tags()->sync($request->tags);
return redirect(route('home'));
}
But why is it complaining about the timestamps, when I removed them from the migration and specified that I'm not using any in the model too? What am I missing?
The submitted "tags" is a multiple select.
答案1
得分: 3
我认为你的错误在于:
$newpost->tags()->sync($request->tags);
我建议查看这个 Laravel 文档,以了解正确的格式应该是:
$newpost->tags()->sync([1, 2, 3]);
或者:
$newpost->tags()->sync([1 => ['expires' => true], 2, 3]);
英文:
I think your error is in:
$newpost->tags()->sync($request->tags);
I would recommend looking at this laravel doc to see that the format should be:
$newpost->tags()->sync([1, 2, 3]);
Or:
$newpost->tags()->sync([1 => ['expires' => true], 2, 3]);
答案2
得分: 1
你尝试在字段 tag_id 中插入 'test' 词,但 tag_id 是 unsignedbiginteger
。
英文:
You tyining instert in field tag_id 'test' word, but tag_id unsignedbiginteger
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论