How to view all database requests for Laravel 5

Source: Internet
Author: User

Description

Laravel has a very flexible database operation scheme, this article introduces a simple method to get all the SQL statements.

1. Turn on QueryLog

We need to call the following methods to tell the framework to start logging SQL requests:

Db::enablequerylog ();

We can consider placing this code in the Beforemiddleware middleware (see here) and using the following command to generate the Beforemiddleware:

PHP Artisan Make:middleware Beforemiddleware

The above command generates a app/http/middleware/beforemiddleware.php file, within the handle method of this file:

Public function handle ($request, Closure $next) {    db::enablequerylog ();    Return $next ($request);}

2. Get QueryLog

After QueryLog is turned on, we can use the following methods to get to the executed SQL:

$queries = Db::getquerylog ();

If you want to get all the QueryLog, you can put the code in Aftermiddleware.

Use the following command to create the Aftermiddleware (document see here):

PHP Artisan Make:middleware Aftermiddleware

A app/http/middleware/aftermiddleware.php file is generated, within the handle method of this file:

Public function handle ($request, Closure $next) {    $response = $next ($request);    Retrieve all executed queries    $queries = Db::getquerylog ();    Code to save query logs in a file    //return response    return $response;    }
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.