CodeIgniter Forums
my querybuilder is not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: my querybuilder is not working (/showthread.php?tid=88322)



my querybuilder is not working - sknair143 - 08-22-2023

Hello,

I'm trying to convert this raw query to query builder :
 Raw query :

Code:
SELECT a.id as ID, a.name as NAME, b.name as DEPT
FROM designations AS a
LEFT JOIN departments AS b ON b.id = a.department_id;
Query Builder :

Code:
  $this->builder()
            ->select('a.id as id, a.name as name, b.name as dept_name')
            ->from('designations AS a')
            ->join('departments as b', 'b.id = a.department_id', 'left')
            ->get();




But query builder is not working as expected, I'm unable to echo the dept_name in the view, it returns null.


RE: my querybuilder is not working - JustJohnQ - 08-22-2023

Maybe
PHP Code:
$db->getLastQuery() 

helps you to find the error in the query.


RE: my querybuilder is not working - HermyC - 08-22-2023

[quote pid="411987" dateline="1692695462"]
Try without the alias..

PHP Code:
$this->builder()
            ->select('designations.id as id, designations.name as name, departments.name as dept_name')
            ->from('designations')
            ->join('departments''departments.id = designations.department_id''left')
            ->get(); 

[/quote]