CodeIgniter Forums
Object of class could not be converted to string - 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: Object of class could not be converted to string (/showthread.php?tid=82594)



Object of class could not be converted to string - jundavewamar - 07-27-2022

Hi, guys I'm a (beginner) in CI4. Can anyone give me advice about this?
I'm trying to pass my getPost value to model, but it says that
Object of class App\Models\Criteria could not be converted to string

controller:
[size=1][font=Consolas, 'Courier New', monospace]  public function dataFilter(){
        // Get Value on select id CatID
        $criteria = new Criteria();
        $CatID = $this->request->getPost('CatID');
        $details= $this->$criteria->fetchDataFilter($CatID); --------- Object of class App\Models\Criteria could not be converted to string
[/font][/size]

[size=1][font=Consolas, 'Courier New', monospace]
        $data['questions'] = $criteria->fetchDataFilter([size=1][font=Consolas, 'Courier New', monospace][size=1][font=Consolas, 'Courier New', monospace]$CatID[/font]
);
     
[/size]        // return response to ajax
        return $this->response->setJSON($data);
    }
[/font][/size]


Model[/font][/size]

[size=1][font=Consolas, 'Courier New', monospace] public function fetchDataFilter($CatID){
        $CatID = $this->request->getPost('CatID');
        $db  = \Config\Database::connect();
        $builder = $db->table('tblcriteria');
        $builder->select('*');
        $builder->join('tblcriteriadetail', 'tblcriteriadetail.CatID = tblcriteria.CatID');
        $builder->where('tblcriteria.CatID', $CatID);
        $builder->where('tblcriteria.status', 1);

        $result = $builder->get()->getResult();
        return $result;

    }



    }[/font][/size]



RE: Object of class could not be converted to string - JustJohnQ - 07-28-2022

Your model returns an object. You try to store this object in the variable $details.
add a line before calling your model:
$details = new stdClass();

Whoops, ignore that. That doesn't make sense. I guess I didn't have enough coffee there.