Welcome Guest, Not a member yet? Register   Sign In
Use Model in view CI4
#1

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!
Reply
#2

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

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(),
]); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB