A
A
Apostol632019-10-17 10:58:43
Laravel
Apostol63, 2019-10-17 10:58:43

Why does __construct throw an error?

Good afternoon
Already broke my head, but I can’t understand the reason
There is a test method of the controller

public function testop() {
    $toQuery = [
      'skip' => 1,
      'onPage' => 5,
      'sort' => 'desc',
      'sortField' => 'id',
      'action' => 'miss',
      'filter' => 'miss',
      'deleteIds' => [],
    ];
    $db = new Querydb('doc_table', $toQuery);
    $db->test();
  }

And there are Querydb model methods, construct and test
public function __construct($dbTableName, $serverParams) {
    $this->table = $dbTableName;
    $this->skip = $serverParams['skip'];
    $this->onPage = $serverParams['onPage'];
    $this->sort = $serverParams['sort'];
    $this->sortField = $serverParams['sortField'];
    $this->action = $serverParams['action'];
    $this->filter = $serverParams['filter'];
    $this->deleteIds = $serverParams['deleteIds'];
  }

public function test() {
    $dataDb = $this->where('status', 1)->orderBy($this->sortField, $this->sort)->skip(0)->take($this->onPage)->get();
    echo '<pre>';
    print_r($dataDb);
    exit();
  }

So the problem is that I get the following error
Too few arguments to function App\Models\Querydb::__construct(), 1 passed in C:\OSPanel\domains\dis\vendor\laravel\framework\src\Illuminate\ Database\Eloquent\Model.php on line 392 and exactly 2 expected
Although I did all sorts of checks I
checked what comes into the construct
In the test method I displayed the properties that are set in the construct (everything is fine with them, they are there and set)
Then I just removed the construct, made a regular method, in which he passed the same variables as in the construct, and in the method itself, the same actions were performed as in the construct. And after this method, I already called the test method. And so everything worked! I'm apparently missing something.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2019-10-17
@Arik

Your problem is precisely that you are inheriting from Model (Illuminate \ Database \ Eloquent)
And you are prompted with an error on line 392, if you open it, you can see that the creation of an instance is started and an array of attributes is passed to it, and you sharpened the constructor for yourself,
I think you are wrong Do you use Lara Models or Lara in general, can you hear the task itself that you are doing?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question