【问题标题】:The given data was invalid给定的数据无效
【发布时间】:2020-11-29 23:59:07
【问题描述】:

我在迁移中添加了以下内容:

$table->string('filePath');

我还确保将它添加到我的填充物中:

protected $fillable = ['user_id','category_id','title','description', 'filePath'];

该字段是必需的,所以我还在我的store 方法中添加了它:

$request->validate([
            'title' => 'required',
            'description' => 'required',
            'filepath' => 'required',
            'category_id' => 'required',
        ]);

当我运行时:php artisan migrate:refresh --seed 我首先得到以下错误:

General error: 1364 Field 'filePath' doesn't have a default value

我还运行了testUserCanCreateAssortment 测试,看看我得到了什么:

Integrity constraint violation: 19 NOT NULL constraint failed: assortments.filePath

在没有--seed 的情况下再次执行相同的命令后,我没有收到任何错误,只是:

table not found

我认为问题出在我的播种机上。现在我补充说:

'filePath' => $this->faker->sentence(40),

在我的assortmentFactory

然后我在运行php artisan migrate:refresh --seed时开始出现以下错误:

string data, right truncated: 1406 Data too long for column 'filePath' at row 1

我变了:

$table->string('filePath');$table->text('filePath');

现在,当我运行我的测试时,我被困在这个问题上:

The given data was invalid.

这是我的testUserCanCreateAssortment 代码:

        $this
            ->followingRedirects()
            ->post(route('assortments.store'), $attributes)
            ->assertSee($attributes['title'])
            ->assertSee($attributes['description'])
            ->assertSee($attributes['file_path']);
        $this->assertDatabaseHas('assortments', $attributes);

我该如何解决这个问题?

编辑

现在我在运行测试时收到以下消息:

Failed asserting that [my html code] ' contains "Corrupti consequatur deserunt non unde dolorem libero distinctio reprehenderit eius perferendis harum eum et dignissimos voluptatem similique architecto iusto expedita nesciunt omnis rerum in sequi aut molestiae tempore est in ut unde tempora omnis similique laborum laudantium aperiam quis et fuga voluptatum cupiditate sunt at rerum mollitia."

【问题讨论】:

  • 请关注naming conventions。模型属性应该是snake_case。你的专栏应该叫file_path
  • 嗨。我将其更改为file_path,但我仍然收到相同的错误。 @mikeroq
  • 你能发布你的 testUserCanCreateAssortment 代码吗?
  • @MarwaneEzzaze 请检查新的编辑
  • @Parsa_237,您能告诉我您在哪个 $attributes 键中保存了 Corrupti consequatur deserunt non unde... 文本吗?

标签: php laravel


【解决方案1】:

在某些环境中,列名区分大小写,因此 filePath 与 filepath 不同。

我建议所有列名都使用小写。

【讨论】:

  • 嗨。我将其更改为file_path,但仍然收到相同的错误。
猜你喜欢
  • 2021-07-30
  • 2020-04-16
  • 2019-09-19
  • 2020-07-01
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
相关资源
最近更新 更多