CodeIgniter Forums
CI4 use model's method in view - 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: CI4 use model's method in view (/showthread.php?tid=76802)

Pages: 1 2


CI4 use model's method in view - pippuccio76 - 06-21-2020

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 ?


RE: CI4 use model's method in view - Mostafa Khudair - 06-21-2020

just call:
PHP Code:
model('CodeigniterModel'); 



RE: CI4 use model's method in view - Digital_Wolf - 06-21-2020

(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.


RE: CI4 use model's method in view - pippuccio76 - 06-21-2020

(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....


RE: CI4 use model's method in view - Digital_Wolf - 06-21-2020

(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?>



RE: CI4 use model's method in view - pippuccio76 - 06-21-2020

(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 ...


RE: CI4 use model's method in view - InsiteFX - 06-22-2020

You would need to do it like this.

PHP Code:
<?php
model
('CodeigniterModel');

$this->codeigniterModel->findById($id);
?>

If you need the id in the view then you can use a hidden form input field.

It is recommended that you do all of this in your controller not the view!


RE: CI4 use model's method in view - pippuccio76 - 06-22-2020

(06-22-2020, 03:06 AM)InsiteFX Wrote: It is recommended that you do all of this in your controller not the view!
but if i have 100 row how can i do this in controller instead view ?


RE: CI4 use model's method in view - InsiteFX - 06-22-2020

Using CodeIgniter Pagination.


RE: CI4 use model's method in view - pippuccio76 - 06-22-2020

(06-22-2020, 08:13 AM)InsiteFX Wrote: Using CodeIgniter Pagination.
NO , how can i get the culumn of foreign key of 100 record 

for example i have i table user:

-name
-surname
-id_club

and a table club

-id
-name

When i show the list of user i don't want show the id_club but i want show the club's name ...

How can i do (without a join) .

In ci3 i can use the model method in view for example (without tag)

table
name surname club

foreach (users as user)

tr
 td user->name td
 td user->surname td
 td this->club_model->findbyid($user->id_club)->name td
tr


how can i do the same in codeigniter 4 ?