CodeIgniter Forums
Use Model in view CI4 - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: Model-View-Controller (
+--- 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.

PHP Code:
<?php  namespace App\Models;

use CodeIgniter\Model;
class MyModel extends Model {

function MyFunction_inModel() {
        return Value;

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();

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