Illuminate prints the executed sql

    Illuminate database is a very powerful and excellent ORM library, and it is also a very practical database operation component. Use it to easily query, insert, update, delete and other operations on the database, support MySQL, Postgres, SQL Server, SQLlite, etc. It is also the database component of the Laravel framework.

This article takes the illuminate database separately, out of the framework, and mainly talks about using the illuminate database query builder to perform database operations.

installation

Use composer to install, directly in the command line of the project root directory, execute

composer require illuminate/database

It is recommended to use PHP^7.2 for the PHP version.

Illuminate can be executed independently in the project. Sometimes we need to print some executed SQL statements. For example, I need to print a SQL statement:

Add before sql execution:

DB::enableQueryLog();

After execution, add:

DB::getQueryLog()

So you can print out the sql statement:

function getGoodList()
{
    DB::enableQueryLog();
    $query = DB::table('goods')
        ->select('goods.name','goods_attribute.value')
        ->join('goods_attribute', 'goods.id', '=', 'goods_attribute.goods_id')
        ->where('goods.id', '1006002')
        ->get();

    echo "<pre>";
    print_r(DB::getQueryLog());
    
    // Write SQL to the log file
    Analog::log(json_encode(DB::getQueryLog()));

    return $query;
}

 

See the effect:

 

Guess you like

Origin blog.csdn.net/lchmyhua88/article/details/107636494