Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of $this->db->last_query() in Codeigniter 4?

I just started learning Codeigniter 4. My query always generates NULL and I don't know why. How can I see the generated SQL Select command just like Codeigniter 3?

In Codeigniter 3 this command does the job:

echo $this->db->last_query();

And this is my controller code in Codeigniter 4 that I need to get the generated query:

$cityModel = new CityModel();
$cities = $cityModel
    ->select('city.name AS cityName')
    ->select('county.name AS countryName')
    ->select('province.name AS provinceName')
    ->join('province', 'city.province_id = province.id', 'left')
    ->join('county', 'city.county_id = county.id', 'left')
    ->result();

Update: I tried this code but it's returning an empty string:

var_export((string)$cityModel->db->getLastQuery());
like image 215
rostamiani Avatar asked Dec 13 '22 09:12

rostamiani


1 Answers

This should display the final query:

$cityModel->getLastQuery()->getQuery()
like image 70
DivPusher Avatar answered Feb 13 '23 23:02

DivPusher