看起来这不是一个新的问题。但我还没有找到任何真正的解决方案。 这是我的代码,期望它能够工作:
$update_pet=pets::where("pet_id",$pet['pet_id'])->get();
if($update_pet->count()>0){
$update_pet=$update_pet->first();
$update_pet->pet_breed_id=$pet_new['pet_breed_id'];
$update_pet->save();
}
我确定$pet ['pet_id']和$pet_new ['pet_breed_id']有值。 我确定数据库中的pet_breeds表主键是pet_id。系统可以连接数据库,因为我可以获取pet_id和新的pet_breed_id。 我确定我已经在模型中重写了表名和primaryKey值。
class pets extends Model
{
protected $table="pets";
protected $primaryKey="pet_id";
}
甚至没有更新。目前我直接使用DB :: update()来运行更新查询以解决问题。但我仍然想知道为什么会发生这种情况?或者是代码有问题吗?或者在更新情况下不能使用保存函数?