【问题标题】:Laravel - Column not found: 1054 Unknown column 'ip_address'Laravel - 找不到列:1054 未知列 'ip_address'
【发布时间】:2021-08-08 17:07:52
【问题描述】:

在 Laravel-8 应用程序中,我正在尝试使用此功能:

public static function addToLog($subject, $logType=null, $subjectHint=null)
{
    $log = [];
    $log['subject'] = $subject;
    $log['log_type'] = $logType;
    $log['subject_hint'] = $subjectHint;
    $log['log_url'] = Request::fullUrl();
    $log['log_method'] = Request::method();
    $log['ip_address'] = Request::ip();
    $log['agent'] = Request::header('user-agent');
    $log['user_id'] = auth()->check() ? auth()->user()->id : 1;
    LogActivityModel::create($log);
}

但是我收到了这个错误:

[2021-05-19 13:25:55] local.ERROR: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ip_address' in 'field list' in C:\xampp\htdocs\myapp \vendor\laravel\framework\src\Illuminate\Database\Connection.php:465

但 ip_address 列存在。

这是我的迁移:

public function up()
{
    Schema::create('user_log_activities', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->nullable()->unsigned();
        $table->string('log_url', 300)->nullable();
        $table->string('log_method', 20)->nullable();
        $table->string('log_type', 50)->nullable();
        $table->string('subject', 200)->nullable();
        $table->string('subject_hint', 20)->nullable();
        $table->string('id_address', 64)->nullable();
        $table->string('agent', 300)->nullable();
        $table->timestamps();
    });
}

如何解决?

谢谢

【问题讨论】:

  • 你的迁移是什么?
  • @brombeer - 我已经添加了迁移
  • 请同时添加 LogActivityModel
  • 您在 Migration 中名为“id_address”的列,而在名为“ip_address”的控制器代码中,您应该将它们联合起来!
  • 他们的列是否存在于您的模型可填充属性中?

标签: laravel


【解决方案1】:

您在迁移中获得了错误的列名,请将其更改为 ip_address 而不是 id_address。像这样的。

 $table->string('ip_address', 64)->nullable();

【讨论】:

    猜你喜欢
    • 2015-02-22
    • 2019-11-11
    • 2018-02-21
    • 2015-12-08
    • 2018-11-02
    • 2019-10-02
    • 2020-05-13
    • 2017-04-03
    相关资源
    最近更新 更多