Search code examples
phplaravelcachinglaravel-5.3

Laravel capture/override fetch/select query events


So I am working on an app where I need to cache specific SELECT queries using any of methods used by laravel such as find, all, select, get, etc.

I know there are events to capture for delete/update/save. Is anything similar there for SELECTish type of queries ? Somewhere I saw one could use newFromBuilder method to capture fetch queries:

public function newFromBuilder($attributes = array())
{
    $instance = parent::newFromBuilder($attributes);

    $instance->fireModelEvent('loaded', true);

    return $instance;
}

But I think above can be used only AFTER query was actually run whereas I want to override any fetch query to add them to cache.

P.S I know I can use CacheTags with the help of some Laravel Pcakages out there but problem is that I don't have any driver that supports cache tags.

Thanks for the help.


Solution

  • Ok I think I figured it out, we can override newBaseQueryBuilder:

    protected function newBaseQueryBuilder()
    {
        $conn = $this->getConnection();
    
        $grammar = $conn->getQueryGrammar();
    
        $builder = new Builder($conn, $grammar, $conn->getPostProcessor());
    
        // do our stuff with builder
    
        return $builder;
    }