php - 错误:找不到列: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。
解决方案
默认情况下,Eloquent 期望created_at
和updated_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
现在检查它是否有效。
推荐阅读
- rule-engine - Grakn 中的规则错误:“规则没有形成有效的子句,因为它包含多原子头”
- apache-kafka - Kafka 消费组描述不包括所有主题
- database - 如何在 Postgres 的多个列中获取查询结果?
- python - python cmd2多参数解析器
- javascript - 替换字符串中的最后一个字符,去掉右边的 0
- python - 熊猫:将数据框列映射到列表并分配二进制值?
- javascript - Body-Parser 不解析 Dates back
- c++ - 计算一个数组的元素个数... 仅仅通过返回类型就可以引用它的索引值吗?什么?如何?为什么?(C++)
- excel - Excel一次复制粘贴到多张工作表中
- git - 在给定 git commit 后查找每个装饰