【问题标题】:Object of class Illuminate\Support\Fluent could not be converted to int类 Illuminate\Support\Fluent 的对象无法转换为 int
【发布时间】:2019-01-28 17:45:10
【问题描述】:

我正在尝试使用 Schema::table 将列添加到现有表中,但无论我做什么都会出现以下错误:

“Illuminate\Support\Fluent 类的对象无法转换为 int”

要点是我不能使用迁移来更新表,因为表是在用户注册时创建的,并且根据他们的选择添加列(15-20 个选择)

这是我为此使用的代码:

Schema::table($new->id, function ($table2) {
-
            $table2->string('choice_one')->default('0');
            $table2->string('choice_two')->default('0');
            $table2->string('choice_three')->default('0');
            $table2->string('choice_four')->default('0');
        });

我尝试到处搜索,但找不到解决方案。

【问题讨论】:

  • 表名应该是单一的(例如:user_choices)并在该表上创建列 user_id 并将用户表的 id 存储在该表上。应该是这样的。
  • 表名是单一的,表名是用户表中的ID所以它总是唯一的
  • Schema::table 结构需要first 参数作为字符串,第二个是闭包。但是您在导致错误的第一个参数中提供了integer。在此处查看详细信息laravel.com/docs/5.6/migrations#columns

标签: sql database laravel schema


【解决方案1】:

尝试传递一个字符串而不是一个整数,比如

Schema::table('t_'.$new->id, function ($table2) {
-
            $table2->string('choice_one')->default('0');
            $table2->string('choice_two')->default('0');
            $table2->string('choice_three')->default('0');
            $table2->string('choice_four')->default('0');
        });

这样你可以传递一个字符串并在将来通过id引用table

【讨论】:

  • 试过了,但它显示了同样的错误,并且 laravel 突出显示以下行:$table2->string('choice_one')->default('0'); 任何想法?
  • 好的,这里有一些问题您使用 Schema::table 来更新架构?您是否返回 Schema::table 代码块?如果您正在创建架构,为什么不使用 Schema::create?如果你能展示更多你的代码,我们可能会有更好的理解
  • 是的,我正在使用 Schema::table 来更新现有表。我在用户第一次注册时使用 schema::create 创建了表。基于设置用户选择,我需要更新表以添加具有他们选择的选项的列。类似于:如果用户选择choice_one 更新他们的表并添加名称为choice_one 的列。如果您需要更多解释,请告诉我
【解决方案2】:

好的,所以最糟糕的编码员噩梦发生了。我在代码中留下了'-',所以它试图将它传递给表。删除后一切正常。 2天的生命过去了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多