Home > Net >  ErrorException: Undefined variable in CodeIgniter 4
ErrorException: Undefined variable in CodeIgniter 4

Time:08-09

I have an ErrorException problem on line 27 of the read_hunter.php file.

The error is as follows: Undefined variable $hunter in <?php foreach($hunter as $hxh): ?>

I don't understand why this is happening, not being able to visualize the number of records in the table in a MySQL Workbench database.

  • read_hunter.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://kit.fontawesome.com/9326dfce3d.js" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>Read hunters</title>
</head>
<body>
    <table >
        <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">Name</th>
                <th scope="col">Year</th>
                <th scope="col">Height</th>
                <th scope="col">Weight</th>
                <th scope="col">Type Hunter</th>
                <th scope="col">Type Nen</th>
                <th scope="col">Type Blood</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($hunter as $hxh): ?> // Erro here of variable undefined
                <th>
                    <td><?php echo['id_hunter']?></td>
                    <td><?php echo['name_hunter']?></td>
                    <td><?php echo['year_hunter']?></td>
                    <td><?php echo['height_hunter']?></td>
                    <td><?php echo['weight_hunter']?></td>
                    <td><?php echo['type_hunter']?></td>
                    <td><?php echo['type_nen']?></td>
                    <td><?php echo['type_blood']?></td>
                    <td>Update | Delete </td>
                </th>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php echo $pager->links();?>
</body>
</html>
  • HunterModel.php
<?php

namespace App\Models;

use CodeIgniter\Model;

class HunterModel extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'hunter';
    protected $primaryKey       = 'id_hunter';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'name_hunter',
        'year_hunter',
        'height_hunter',
        'weight_hunter',
        'type_hunter',
        'type_nen',
        'type_blood'
    ];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];
}
  • HunterController.php
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\HunterModel;

class HunterController extends BaseController
{
    private $hunter;

    public function __construct()
    {
        $this->hunter = new HunterModel();
    }

    public function readHunters()
    {
        return view('read_hunter', [
            'read_hunter' => $this->hunter->paginate(10),
            'pager' => $this->hunter->pager
        ]);
    }
}

CodePudding user response:

You just declare $hunter in controller but not send in view

public function readHunters()
{
    return view('read_hunter', [
        'hunter' => $this->hunter,
        'read_hunter' => $this->hunter->paginate(10),
        'pager' => $this->hunter->pager
    ]);
}
  • Related