master

laravel/framework

Last updated at: 29/12/2023 09:22

ExplainsQueries.php

TLDR

The file ExplainsQueries.php contains a trait called ExplainsQueries. This trait provides a method called explain() which can be used to explain the query. The method retrieves the SQL statement from the query, gets the bindings, executes an explain query on the database connection, and returns the explanation as a collection.

Methods

explain

This method explains the query by retrieving the SQL statement and bindings from the current query, executing an explain query on the database connection, and returning the result as a collection.

Classes

None

<?php

namespace Illuminate\Database\Concerns;

use Illuminate\Support\Collection;

trait ExplainsQueries
{
    /**
     * Explains the query.
     *
     * @return \Illuminate\Support\Collection
     */
    public function explain()
    {
        $sql = $this->toSql();

        $bindings = $this->getBindings();

        $explanation = $this->getConnection()->select('EXPLAIN '.$sql, $bindings);

        return new Collection($explanation);
    }
}