首页 > 解决方案 > 错误:找不到列:1054“字段列表”中的未知列“updated_at”

问题描述

所以基本上错误表明我没有名为 updated_at 的列,但我知道我没有它,我不想拥有它。我的数据库表只有以下列:requester、user_requested、id、status。

这是我的模型

class Friendships extends Model
{
    protected $fillable = ['requester', 'user_requested', 'status'];
}

这是我的配置文件控制器

 public function sendRequest($id){
        return Auth::user()->addFriend($id);

  }

这是我的friendable.php


namespace App\Traits;
use App\Friendships;

trait Friendable{
    public function test(){
        return 'hi' ;
    }

    public function addFriend($id){
        $Friendship = Friendships::create([
                'requester' => $this->id,
                'user_requested' => $id
        ]);


        if($Friendship){
            return $Friendship;
        }
        return  'failed';
    }


}

它说方法 addFriend 没有找到,并且 create 显然不起作用,因为没有找到 id。

标签: phphtmllaravel

解决方案


默认情况下,Eloquent 期望created_atupdated_at列存在于您的表中。如果您不希望 Eloquent 自动管理这些列,请将 $timestamps模型上的属性设置为false

class Friendships extends Model
{
    public $timestamps = false;
    protected $fillable = ['requester', 'user_requested', 'status'];
}  

编辑

开火命令,

php artisan make:migration modify_friendships_table;

然后转到生成的迁移文件database/migrations

在该类中编写代码,

Schema::table('friendships', function (Blueprint $table) {
    $table->boolean("status")->default(0)->change();
});

保存上面的文件,然后在下面的命令中触发

php artisan migrate

现在检查它是否有效。


推荐阅读