gpt4 book ai didi

php - 没有主键的 Laravel 模型保存数据

转载 作者:IT王子 更新时间:2023-10-29 00:03:01 25 4
gpt4 key购买 nike

我有 2 个模型:Profile 和 Student。配置文件包含一个主键 id。 Student 模型没有任何主键,只有一个外键,profile_id,它链接到 profile.id。我的学生 Controller 中有一个函数可以激活学生,在调用时将状态从 0 设置为 1。我正在尝试使用 save() 来更新值,但无法执行此操作,提示我这样一条消息:

Column not found: 1054 Unknown column 'id' in 'where clause'

可以请教吗?非常感谢。

学生模型:

class Student extends Eloquent {

protected $primary_key = null;
public $incrementing = false;

protected $fillable = array('username', 'password', 'status', 'profile_id');

public function profile() {
return $this->belongsTo('Profile');
}
}

资料模型:

class Profile extends Eloquent {

protected $fillable = array('firstname', 'lastname', 'email', 'phone');

public function student()
{
return $this->hasOne('Student', 'profile_id', 'id');
}
}

学生 Controller :

public function activate($id) {
$student = Student::where('profile_id', '=', $id)->first();
$student->status = 1;
$student->save();
return Redirect::to('students');
}

最佳答案

您可能需要考虑添加一个 id 字段。这对 Laravel 和包非常重要(特别是对于聚合)

但是如果你必须...

学生模型:(驼峰式 primaryKey 属性)

class Student extends Eloquent {

protected $primaryKey = null;
public $incrementing = false;


protected $fillable = array('username', 'password', 'status', 'profile_id');

public function profile() {
return $this->belongsTo('Profile');
}
}

关于php - 没有主键的 Laravel 模型保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com