Laravel 5 Eloquent: How to get raw sql that runs? (with related data)

I am trying to figure out how to execute a raw SQL query, including the data associated with it. Here is what ive got:

\DB::connection()->enableQueryLog();
$query = \DB::getQueryLog();
$lastQuery = end($query);

And here is the result:

array(3) {
  ["query"]=>
  string(57) "select * from `table_1` where `field_1` = ? limit 1"
  ["bindings"]=>
  array(1) {
    [0]=>
    string(34) "xyz"
  }
}

So, how do I get a dump of a full sql query like this (good old fashioned way)?

select * from `table_1` where `field_1` = 'xyz' limit 1

thank

+4
source share
3 answers

Add this to your routes. Folder:

\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
    Log::info( json_encode($query->sql) );
    Log::info( json_encode($query->bindings) );
    Log::info( json_encode($query->time)   );
});

Log :: info () ==> will log an SQL query in the file storage / logs / laravel.log

var_dump () ==> will be displayed in the output API call

+1
source

Try adding an event listener for the query:

Event::listen('illuminate.query', function($query)
{
    var_dump($query);
});

or

$results = User::where('id',$id)->toSql();
dd($results)
0
source

Laravel debugbar. Laravel . ( ajax) , , ​​ /, . ( TONS , , , , ..)

, Laravel toSql(), . , , . toSql(), , .

$foo = Foo::where('bar', 'baz');
$foo_sql = $foo->toSql();
$foo->get();
0

Source: https://habr.com/ru/post/1653301/


All Articles