为什么我无法在Laravel中不使用时间戳?

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 17:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定