Welcome Guest, Not a member yet? Register   Sign In
"$this->request->getVar" in a model with CI4
#2

(01-23-2021, 01:01 PM)divebase Wrote: I have tried to use
"$this->request->getVar" in a model.
I didn't succeed.
It only worked when I moved the construct into a controller.
Why? What's my mistake?

If you need to use Request in a model method, pass a Request instance as a parameter to the method.
Or you can pass to the model constructor.
Or you can contact through the service.
There are many ways.
PHP Code:
$model = new SomeModel();
$model->someMethod($this->request);
//
$model = new SomeModel($this->request);
//
$request App\Config\Service::request(); 


(01-23-2021, 01:01 PM)divebase Wrote: Then I tried "$this->db->update($id, $data);" to use in a model.
Error message:
Call to undefined method CodeIgniter \ Database \ MySQLi \ Connection :: update ()
Why?
How do I introduce the method to the model?

The $db property contains the connection.
To use QueryBuilder call $this->builder()
PHP Code:
$builder $this->builder();
$builder->update(....); 
// or
$this->builder()->update(....); 
Reply


Messages In This Thread
RE: "$this->request->getVar" in a model - by iRedds - 01-24-2021, 03:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB