Search code examples
phpcodeigniter-4

Null values instead of an Value in CodeIgniter


I have this code that is returning a NULL instead of a value. Here is my Controller with the name Agents, this code runs on the Admin side but when I used the code for Agents user levels it's returning NULL:

<?php namespace App\Controllers;


use App\Models\ClientModel;

class Agent extends BaseController{


    public $modelClients;


    public function __construct(){

        $this->modelClients = new ClientModel();



    }

function getClientsToAgent($id){

        $data = ([          
            "title" => "Lead Generation",
            "client" => $this->modelClients->where("clientsId", $id)->first(),
            "tasks" => $this->modelTasks->orderby("taskId", "ASC")->findAll()
        ]);
        echo view('agentTemplate/header',$data);
        echo view('agents/loadLeadGenView');
        echo view('agentTemplate/footer');
    }

}

and on my view named loadLeadGenView:

 <?php 

    var_dump($client);
 ?>

enter image description here

here's my Model name ClientModel:

<?php namespace App\Models;

use CodeIgniter\Model;

class ClientModel extends Model{

    protected $table = "tblclients";

    protected $primaryKey = "clientsId";

    protected $returnType = 'array';

    protected $allowedFields = ["clientsId","clientsFirstname","clientsMiddlename", "clientsLastname","clientsBusinessName","clientsCampaignGoals","clientsDateStarted","clientsJointVenture","clientsEmailAddress"];

}

Another this on my Routes.php:

$routes->get('agents/getClientsToAgent/(:num)', 'Agents::getClientsToAgent/($1)');

that is why I'm wondering. why it's returning NULL instead of a value?


Solution

  • fix your route, remove brackets around $1

    $routes->get('agents/getClientsToAgent/(:num)', 'Agents::getClientsToAgent/$1');

    https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#examples