Laravel Eloquent display query log

后端 未结 9 1761
萌比男神i
萌比男神i 2020-11-30 04:46
use App\\Order;

public function show(Order $order){

        $data = $order->all();
        return dd($order->getQueryLog());

Is there any w

相关标签:
9条回答
  • 2020-11-30 05:09

    You can use my Laravel package which is work perfectly in Larave 6 and 7. (Not sure about laravel 5 or less.)

    For use Install the package via composer: by following command

    composer require dipenparmar12/laravel-query-log

    restart server. after this you will see

    storage/logs/db-query.log file containing your query logs.

    For more details visit Laravel-query-log

    Thanks

    0 讨论(0)
  • 2020-11-30 05:13

    I know it's an old question, but it might help others that had the same issue I had.

    If you use other connection than the default one, you should specify it to get the query log properly.

    \DB::connection('YourConnection')->enableQueryLog();
    $test = MyModel::all();
    $queries = \DB::connection('YourConnection')->getQueryLog();
    dd($queries);
    
    0 讨论(0)
  • 2020-11-30 05:15

    First you have to enable query log it can be done using

    DB::connection()->enableQueryLog();
    

    then you can use below code to see the query log

    $queries = DB::getQueryLog();
    

    if you want to see the last executed query

    $last_query = end($queries);
    

    to know more about logging see this https://laravel.com/docs/5.0/database#query-logging

    Example

    public function show(Order $order){
            \DB::connection()->enableQueryLog();
            $data = $order->all();
            $queries = \DB::getQueryLog();
            return dd($queries);
    }
    
    0 讨论(0)
  • 2020-11-30 05:16

    To see the query logs in laravel.log file use the following way.

    namespace App\Providers;
    
    use DB;
    use Log;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            DB::listen(function($query) {
                Log::info(
                    $query->sql,
                    $query->bindings,
                    $query->time
                );
            });
        }
    
        // ...
    }
    
    0 讨论(0)
  • 2020-11-30 05:16

    You can use this package https://github.com/supliu/laravel-query-monitor

    Após instalar, abra o terminal e execute o comando:

    php artisan laravel-query-monitor
    

    All queries executed by Eloquent will be displayed in real time

    0 讨论(0)
  • 2020-11-30 05:19

    Working on 5.6, something like this in AppServiceProvider::boot()

        // Log all DB SELECT statements
        // @codeCoverageIgnoreStart
        if (!app()->environment('testing') && config('app.log_sql')) {
            DB::listen(function ($query) {
                if (preg_match('/^select/', $query->sql)) {
                    Log::info('sql: ' .  $query->sql);
                    // Also available are $query->bindings and $query->time.
                }
            });
        }
    

    Then in config/app.php, just so it's easy to enable/disable from amending the .env

        'log_sql' => env('LOG_SQL'),
    

    All credit to: https://arjunphp.com/laravel-5-5-log-eloquent-queries/

    And this can be parsed for unique queries with:

        grep ") sql:" laravel.log | sed -e "s#.*select\(.*\)\[\]#select\1#" | sort -u
    
    0 讨论(0)
提交回复
热议问题