Ad

How To Display Last Query In Laravel 5?

- 1 answer

I use :

DB::connection()->enableQueryLog();
var_dump(DB::getQueryLog());

But it's not working.

My service is like this :

public function dataCustomerList($param) 
    {
        $status = $param['status_sort'];
        $gender = $param['gender'];
        $published = $param['published'];

        if($published=='NO'){
            $published_check = 'NO';
            $published = 0;
        }
        else if($published=='YES'){
            $published_check = 'YES';
            $published = 1;
        }
 DB::connection()->enableQueryLog();
        $customer = customer::paginate(10);

        if(!empty($status)) {
            // die($status);
            $customer = customer::where('tb_customer.customer_status', '=', $status)->paginate(10);
        }

        if (!empty($gender)) {
            $customer = customer::where('tb_customer.gender', '=', $gender)->paginate(10);
        }

        if (!empty($published_check)) {
            $customer = customer::where('tb_customer.published', '=', $published)->paginate(10);
        }
        var_dump(DB::getQueryLog());
        return $customer;


    }   

When I run it, query does not appear

The result is like this :

array(0) {
}

How to get the query executed in Laravel 5?

Thank you very much

Ad

Answer

You could listen to query events on DB:

DB::listen(function($query) {
    var_dump($query->sql);
});
Ad
source: stackoverflow.com
Ad