CI4 How to load a model by sending params |
I have a controller that loads a model and I need to pass parameters
PHP Code: namespace App\Controllers; How can I send parameters to the model? I tried with: PHP Code: $this-Mymodel = model('App\Models\Mymodel', true, ['123', 'abc']);
On top of your controller call your Model
PHP Code: use App\Models\YourFolderNameForModels\UsersModel; //for example user models You can call it in your __construct like you did, but be careful: PHP Code: $this->UserModel = new UsersModel(); Then you can use it : Code: $data = [
(02-14-2022, 04:50 AM)demyr Wrote: On top of your controller call your Model So yes, I would like to pass parameters to the class instantiation using PHP Code: model('mymodel');
Sadly, you can't. `model()`'s 2nd and 3rd parameters are fixed to accept instances of ConnectionInterface and ValidationInterface.
https://github.com/codeigniter4/CodeIgni...#L809-L812
2nd parameter is $getShared.
3rd parameter is ConnectionInterface. Do you want to do like this? Mymodel does not extend CodeIgniter\Model. PHP Code: $this->Mymodel = new \App\Models\Mymodel('123', 'abc'); |
Welcome Guest, Not a member yet? Register Sign In |