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

You need to assign the complete model to a variable.

PHP Code:
<?php namespace App\Controllers;

use 
App\Models\ExampleModel;

class 
Example extends BaseController
{
    public function 
example()
    {
        
$exampleModel = new ExampleModel();

        
$data = [
            
'exampleModel' => $exampleModel,
        ];

        echo 
view('example'$data);
    }


You can then access it with $exampleModel->findbyid() in your view.
Reply
#12

(This post was last modified: 08-21-2020, 09:28 AM by jinbatsu.)

(06-22-2020, 01:20 PM)pippuccio76 Wrote: 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 ?

Actually even in CI3, I use it either in Model or Controller, not in view.
In CI3, I use like this in Model/Controller:

PHP Code:
function get_data_grid($param) {
    
// just example can use as pagination etc..
    
$offset = isset($_POST['offset']) ? $_POST['offset'] : 0;
    
$limit = isset($_POST['limit']) ? $_POST['limit'] : 10;
    
$search = isset($_POST['search']) ? $_POST['search'] : '';

    
$sql "SELECT * FROM users";
    
$query $this->db->query($sql);
    
$data_list $query->result();

    
$data_list_e = array();

    
// generate real output
    
foreach ($data_list as $val) {
        
// now we can add new value in this row
        
$val->club_info $this->get_club_info($val->club_id);

        
$data_list_e[] = $val;
    }
    
$total "TOTAL QUERY"// just example

    
$out = array('rows' => $data_list_e'total' => $total);
    return 
$out;    
}

function 
get_club_info($id) {
    
// SQL query..
    
return $result;


In view we can use this:
PHP Code:
foreach ($rows as $val) {
    echo 
$val->username;
    
$club_info $val->club_info;
    foreach (
$club_info as $row_club) {
        echo 
$row_club->club_name;
    }


And I think this should be okay with CI4.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB