CodeIgniter Forums
Use Model in view CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Use Model in view CI4 (/showthread.php?tid=76029)



Use Model in view CI4 - gra - 04-08-2020

How can you use a model in a view.

Model:
PHP Code:
<?php  namespace App\Models;

use CodeIgniter\Model;
class MyModel extends Model {

function MyFunction_inModel() {
        return Value;
    }




View:
PHP Code:
??? MyModel->MyFunction_InModel(); 


Thank you!


RE: Use Model in view CI4 - jreklund - 04-08-2020

I haven't tried it myself. But you need to add the complete object inside your $data array and pass it into the view.


RE: Use Model in view CI4 - kilishan - 04-08-2020

Best practice is to use the model within your controller to get the data you need, and pass it to the view. It is usually recommended to keep as little function calling out of the views. They should be just for display purposes with as little logic as possible.

PHP Code:
$users = new UserModel();

echo 
view('users/index', [
    
'users' => $user->findAll(),
]);