smchae
smchae

Reputation: 1095

How to use construct method in Model

I am new to laravel. I want to override construct method in my model, however, when I try to use the construct method inside my model, it returns an error saying Call to undefined method Illuminate\Database\Query\Builder::construct()

The purpose of using __construct method is that I want to use DB table dynamically.

What is the problem? How could avoid this error?

Model

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);
}

}

Controller

$custom = new Custom($tableName);
$result = $custom->create($data);

Upvotes: 0

Views: 77

Answers (1)

Federkun
Federkun

Reputation: 36924

You probably mean to use parent::__construct($attributes); instead of parent::construct($attributes);.There's no construct method, like the error message said.

Upvotes: 4

Related Questions