Search code examples
codeigniter-4

Builder could not be converted to string


Need help, I want to sum one column in codeigniter4 but I get this error:

Error: object of class CodeIgniter\Database\MySQLi\Builder could not be converted to string

This is my model:

public function tot_crew_wni_arr() {
    return $this->db->table('tbl_est_arr')->selectSum('crew_wni_arr');
}

at controller

$data = array(
'tot_crew_wni_arr' => $this->Model_home->tot_crew_wni_arr(),
);
return view('layout/v_wrapper', $data);

This is my view:

<h3><?= $tot_crew_wni_arr ?></h3>

Solution

  • Your model function should look something like this:

    public function tot_crew_wni_arr() {
        $db      = \Config\Database::connect();
        $builder = $db->table('tbl_est_arr');
        return $builder->selectSum('crew_wni_arr')->get();
    }