gpt4 book ai didi

php - Laravel 构造函数的 Eloquent 问题

转载 作者:行者123 更新时间:2023-12-02 04:23:50 28 4
gpt4 key购买 nike

我有一个包含许多方法的模型。

class UserModel extends Eloquent{

private $active;

function __construct() {
$this->active = Config::get('app.ActiveFlag');
}

protected $table = 'User';
protected $fillable = array('usr_ID', 'username');

public function method1(){
//use $active here
}

public function method2(){
//use $active here
}

}

Controller :

$user = new UserModel($inputall);
$user->save();

没有构造函数,它可以正常工作。但是,使用构造函数时,它不会保存用户(生成的查询没有任何填充属性或值)。查询如下:

 insert into User() values();

请问有什么意见吗?

最佳答案

是的,那是因为您重写了 Eloquent 构造函数,该构造函数负责在传递数组时用值填充模型。您必须使用 parent::__construct() 将它们传递给父级:

public function __construct(array $attributes = array()){
parent::__construct($attributes);
$this->active = Config::get('app.ActiveFlag');
}

关于php - Laravel 构造函数的 Eloquent 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471457/

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