如何在Model中使用构造方法

问题描述 投票:0回答:1

我是laravel的新手。我想在我的模型中覆盖构造方法,但是,当我尝试在我的模型中使用构造方法时,它返回一个错误说Call to undefined method Illuminate\Database\Query\Builder::construct()

使用__construct方法的目的是我想动态使用DB表。

问题是什么?怎么可以避免这个错误?

模型

class Custom extends Model
{
protected $guarded = ['id', 'ct'];

const UPDATED_AT = null;
const CREATED_AT = 'ct';

public function __construct(string $tableName = null, array $attributes = []) {

    $this->setTable($tableName);

    parent::construct($attributes);
}

}

调节器

$custom = new Custom($tableName);
$result = $custom->create($data);
php laravel laravel-5
1个回答
4
投票

你可能意味着使用parent::__construct($attributes);而不是parent::construct($attributes);.There没有construct方法,就像错误消息所说。

© www.soinside.com 2019 - 2024. All rights reserved.