0 (+91) 98328-93116 Monday - saturday, 10am - 8pm
scriptbazar@gmail.com Drop us a line anytime!
infolinematrix Monday - saturday, 10am - 8pm
toggle menu
0 (+91) 98328-93116 Monday-Saturday, 10am - 8pm
info@scriptbazar.com Drop us a line anytime!
infolinematrix Monday-Friday, 11am - 7pm
toggle menu

How to avoid duplicate queries in Barryvdh's Debug Bar  in Laravel ?

If you are a Laravel developer then I am sure you are using Barryvdh's Debug Bar or any other semilar package to debugging your apllication. And you find many of duplicate queries, which is slowdown your site rendering on browser, and you simply don't have the time to track them all down.

OK, I have found a solution, which I have tested and implimented in our all the products, really, It did not execute and any duplicate queries and my site rendering more fast. Today I will share it here..


STEP 1 :
Every SELECT query in Laravel, whether it's using Eloquent, or the DB Facade, all runs through a single function. We're going to override that function.

Create a file Builder.php , Path hints “app/Support/Database/Builder.php

write the following code



namespace App\Support\Database;
use Cache;
use Illuminate\Database\Query\Builder as QueryBuilder;

class Builder extends QueryBuilder
{
    /**
    * Run the query as a "select" statement against the connection.
    *
    * @return array
    */
    protected function runSelect()
    {
        return Cache::store('request')->remember($this->getCacheKey(), 1, function() {
            return parent::runSelect();
        });
    }

    /**
    * Returns a Unique String that can identify this Query.
    *
    * @return string
    */
    protected function getCacheKey()
    {
        return json_encode([
            $this->toSql() => $this->getBindings()
        ]);
    }
}


And Save it..


STEP 2 :
Now, we need to set up a custom Cache Driver. We're doing this so that we don't mess with any existing Cache logic you might already be using. Here's how I've defined this:

Edit the /config/cache.php file and edit the following line.



'stores' => [
    'request' => [
        'driver' => 'array'
    ]
]




STEP 3 :

Edit the public/index.php file and put the following line at the end of file.



try {

// Flush the Request Cache

$app->make('cache')->store('request')->flush();

 }

catch(ReflectionException $ex) {

// Do nothing

}


STEP 4:
Now create a Trait file CacheQueryBuilder.php,
Path : /app/Support/Database/CacheQueryBuilder.php.


namespace App\Support\Database;

trait CacheQueryBuilder
{
    /**
    * Get a new query builder instance for the connection.
    *
    * @return \Illuminate\Database\Query\Builder
    */
    protected function newBaseQueryBuilder()
    {
        $conn = $this->getConnection();

        $grammar = $conn->getQueryGrammar();

        return new Builder($conn, $grammar, $conn->getPostProcessor());
    }
}


Now use the Trait (CacheBuilder.php) to all your model
Example :


namespace App;

use App\Support\Database\CacheQueryBuilder;
use Illuminate\Database\Eloquent\Model;

class MyModel extends YourModel
{
    /**
    * For Caching all Queries.
    */
    use CacheQueryBuilder;
}


Thats all ...


Comments

We welcome your comments, questions, corrections and additional information relating to this article. Your comments may take some time to appear. Please be aware that off-topic comments will be deleted.

If you need specific help with your account, feel free to contact our Support Team. Thank you.

Follow us

Get Help

Our experts are available to answer your questions and discuss your project needs.

Team Manager
Subha Team Manager