Welcome Guest, Not a member yet? Register   Sign In
CI4 use model's method in view
#6

(06-21-2020, 11:29 AM)Digital_Wolf Wrote:
(06-21-2020, 11:15 AM)pippuccio76 Wrote:
(06-21-2020, 10:56 AM)Digital_Wolf Wrote:
(06-21-2020, 07:06 AM)pippuccio76 Wrote: Hi ,sorry for engish , in ci3 if i have a model call codeigniterModel with a method call findById($id) 

in controller i load model with :

PHP Code:
$this->load->model('codeigniterModel'


and in view i can use   $this->codeigniterModel->findById($id) method 

how can i do the same on ci4 ?


Hello !

You can do it like this:
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

// use u model class.
use App\Models\codeigniterModel;

class 
MyController extends Controller
{
    public function index()
    {
        // create instance u model.
        $CI_Model = new codeigniterModel();

        // call the method from model
        echo $CI_Model->findById($id);
    

p.s. Sorry my English.
I want use in a view not in a controller....

Then you just need to slightly modify the code in the controller itself like this:
PHP Code:
...here everything that I wrote above...
// create instance u model.
$CI_Model = new codeigniterModel();
// create new data "storage"
$data = [
     'result_query' => $CI_Model->findById($id)
];
// passing data to view
echo view("your_view_file"$data); 
Further, in the view file itself, you need to call the data simply by accessing the key:
PHP Code:
<?= $result_query?>
no,you don't understand  , i want use model's functions in view , as ci3 .....
for example : in db table there are foreign key , in  the page where i show the list , i cannot write the id of the foreign key but i must insert for example the name ...
Reply


Messages In This Thread
CI4 use model's method in view - by pippuccio76 - 06-21-2020, 07:06 AM
RE: CI4 use model's method in view - by pippuccio76 - 06-21-2020, 11:09 PM
RE: CI4 use model's method in view - by InsiteFX - 06-22-2020, 03:06 AM
RE: CI4 use model's method in view - by InsiteFX - 06-22-2020, 08:13 AM
RE: CI4 use model's method in view - by jreklund - 06-22-2020, 01:58 PM
RE: CI4 use model's method in view - by jinbatsu - 08-21-2020, 09:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB