Search
 
SCRIPT & CODE EXAMPLE
 

PHP

How to Log Query in Laravel

DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());
Comment

How to Log Query in Laravel

DB::enableQueryLog();
$user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());
Comment

how get query logs in laravel

Method #1
instead of ->get use ->toSql() on query 
$users = User::orderBy('name', 'asc')->toSql();

echo $users;

// Outputs the string:
'select * from `users` order by `name` asc'
Method # 2

DB::enableQueryLog();

// and then you can get query log

dd(DB::getQueryLog());
Comment

laravel enable query log


use IlluminateSupportFacadesDB;
...
...
 
public function UserController()
{
    DB::enableQueryLog();
    $arr_user = DB::table('users')->select('name', 'email as user_email')->get();
    dd(DB::getQueryLog());
}
Comment

laravel log query for model

public function show(Order $order){
    DB::connection()->enableQueryLog();
    $data = $order->all();
    $queries = DB::getQueryLog();
    return dd($queries);
}
Comment

laravel log query for model (full)

namespace AppProviders;

use DB;
use Log;
use IlluminateSupportServiceProvider;

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
            );
        });
    }

    // ...
}
Comment

query log laravel

import DB with this line first, 
use IlluminateSupportFacadesDB;
Comment

laravel enable query log

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

query log laravel

check whethe the serviceprovider is added in the providers array in config/app.php
 if no then check whether the service provider class is the correct location and 
   include the serivce provider in the providers array in config.app.php
   
Comment

query log laravel

check whethe the serviceprovider is added in the providers array in config/app.php
 if no then check whether the service provider class is the correct location and 
   include the serivce provider in the providers array in config.app.php
   
Comment

larave log all query

DB::listen(function ($query) {
    var_dump([
        $query->sql,
        $query->bindings,
        $query->time
    ]);
});
Comment

PREVIOUS NEXT
Code Example
Php :: how to remove public from url in codeigniter 4 
Php :: php console log array 
Php :: laravel model without timestamps 
Php :: tinker lost color cli 
Php :: check if session is started php 
Php :: error reporting in php 
Php :: php curl print status 
Php :: laravel get project root 
Php :: wp cron disable 
Php :: php remove numbers from string 
Php :: laravel migrate only 1 file 
Php :: wordpress max memory limit 
Php :: how to get file extension in laravel 
Php :: php get last modified date of file 
Php :: if post php 
Php :: laravel catch exception ex log save 
Php :: write to file php 
Php :: laravel order by desc 
Php :: laravel running a specific migration on different path 
Php :: Delete Query with Where Condition in Codeigniter 
Php :: how to add php 7.4 in homebrew 
Php :: You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org 
Php :: hide php extension htaccess 
Php :: php save array to file 
Php :: acf link 
Php :: laravel scout import command 
Php :: php check if url exists 
Php :: wp get image alt 
Php :: check if constant is defined php 
Php :: php difference between two dates in years months and days 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =