Print last executed query in laravel

Updated at 14-Mar-2021, By samar

Print last executed query in laravel

In this session, we’ll try our hand at solving the "Print last executed query in laravel" puzzle by using the computer language.

  • --PATH app\Http\Controllers\<YourController.php>
    DB::enableQueryLog();
    //Change models name as per your requirement and do not forget use model in your controller file 
    $employee = Employee::all();
    $employeeDetail = EmployeeDetail::all();
    $queries = DB::getQueryLog();   
    //Get the last query
    $last_query = end($queries);
    dd($last_query);
    
    This code snippet will print last executed query in laravel in the form of raw SQL. There are two queries executed on Employee and EmployeeDetail models. But it will only show the query details (raw SQL query) of the EmployeeDetail model which is executed after the Employee model in our case.

Back to code snippet queries related laravel