How to pass data from controller to models in codeigniter?

Member

by rollin , in category: PHP Frameworks , 2 months ago

How to pass data from controller to models in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 2 months ago

@rollin 

In CodeIgniter, you can pass data from controllers to models by creating a new method in the model and passing the data as a parameter to that method. Here's an example:


In your controller:

1
2
3
4
5
6
7
8
$this->load->model('Your_model');

$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

$this->Your_model->your_method($data);


In your model (Your_model.php):

1
2
3
4
5
6
7
class Your_model extends CI_Model {

    public function your_method($data) {
        // Do something with the data
    }

}


Inside the your_method method, you can access the data passed from the controller as the parameter $data and perform any necessary operations on it. This allows you to pass data from controllers to models in CodeIgniter.