【问题标题】:Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054使用消息'SQLSTATE [42S22] 照亮/数据库/查询异常:找不到列:1054
【发布时间】:2019-05-26 00:33:12
【问题描述】:

我尝试使用 php artisan tinker 创建管理员用户。当我尝试保存时,它返回错误。请帮我解决问题。

这是错误

使用消息“SQLSTATE[42S22] 照亮/数据库/查询异常: 未找到列:1054“字段列表”中的未知列“管理员”(SQL: 更新users 设置updated_at = 2018-12-28 05:41:31,admin = 1 其中id = 1)'

【问题讨论】:

  • User 表中没有admin 列。
  • @Saji 显示你的 User 有问题的模型

标签: laravel eloquent


【解决方案1】:

您的数据库的“用户”表中没有名为“管理员”的列。添加一个“管理员”列。 这应该可以解决错误。

【讨论】:

  • 完成。但随后返回另一个错误“PHP 错误:在第 1 行的 Psy Shell 代码中调用未定义的方法 stdClass::save()”
  • 试试这个 $me = new App\User $me = App\User::find(1) $me->admin = 1 $me->save()
【解决方案2】:

请删除$fillable中的admin,然后重试

【讨论】:

    【解决方案3】:

    您必须创建一个迁移,其中包含要添加到数据库架构的新列。

    示例:(这将添加一个 TINYINT admin 列,并将 0 作为默认值)

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class AddsAdminColumnToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->tinyInteger('admin')->default(0);
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->dropColumn('admin');
            });
        }
    }
    

    Read more about Laravel Migrations here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2019-03-08
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多