Introduction to the use of Laravel Debugging Tools Laravel-debugbar

Source: Internet
Author: User

With the Debugbar debugging tool installed for the Laravel project, a toolbar opens at the bottom of the page in debug mode, where you can view performance statistics and, most importantly, print the SQL statements and debug content that the page executes. The following uses the Debugbar tool to find the optimization point for a SQL query.

Optimizing Page SQL queries

Here are two models, destination models and travel models, they are one-to-many relationships defined in the model destination.php of the destination models as follows:

Php

/**
* Define destination-a one-to-many relationship of travels
* @return \illuminate\database\eloquent\relations\hasmany
*/
Public Function Travel ()
{
return $this->hasmany (travel::class, ' destination_id ');
}

In order to count the number of travels in each destination, the Laravel model uses a very useful feature appends, the definition appends array is the custom table field:

Php

/**
* Custom Fields
* @var Array
*/
protected $appends = [
' URL ',
' First_travel_url ',
' Total ',
];


Here total refers to the destination of the number of travel notes, the code before the optimization is such:


/**
* Destination Travel number
* @return Mixed
*/
Public Function Gettotalattribute ()
{
return $this->travel ()->count ();
}
This results in traversing the total number of travel destinations and increasing the frequency of SQL statements. This would have been a good feature, but additional query overhead might be added if you were to query the database. The Debugbar tool prints all the SQL statements that the page executes on the page, as shown in the figure:

Obviously it needs to be optimized here, and it needs to be counted by a group by destination, just a single SQL statement.

Used in Laravel
Using GROUP by for statistics

Add a method to the model of the travel travel.php:

/**
* Get the total number of travels per destination
* @return Array
*/
Public Function gettravelnumsgroupbydestination ()
{
$travelNums = [];
$result = $this->groupby (' destination_id ')->get ([' destination_id ', Db::raw (' count (*) as Total ')];
foreach ($result as $item) {
$travelNums [$item->destination_id] = $item->total;
}
return $travelNums;
}
Here $result returns a Collection object with a collection of Travel classes in which the SQL statement actually executes: select DESTINATION_ID, COUNT (*) as total from Travel Group by destination_id

Group-by-select-count

Call this method in the index method of the page controller travelcontroller.php, and then assign the value to the template.

Php

/**
* Travel Home
* @return \illuminate\contracts\view\factory|\illuminate\view\view
*/
Public Function Index ()
{
$data = Cache::remember (' Travel.index ', self::cache_time, function () {
$destinationList = $this->destination->getlist (12);
$travelList = $this->travel->latest (' begin_date ')->take ()->get ();
$travelNums = $this->travel->gettravelnumsgroupbydestination ();
return [
' Destinationlist ' => $destinationList,
' Travellist ' => $travelList,
' Travelnums ' => $travelNums
];
});
Return view (' Travel.index ', $data);
}
Here the database query results are cached, only in the cache expiration will query the database.

After making the appropriate modifications in the template, this SQL is optimized for performance, which reduces the number of SQL queries if more queries are available.

Optimized after page execution sql:

Through the Debugbar tool can very good grasp of the performance, not only that, he is a good debugging tool, we can print log way to debug the code, can also use Debugbar to provide debugging methods to print content, very convenient, using laravel development of the necessary debugging tools.

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.