M
M
Maxim Zolotoy2019-05-08 22:49:25
Laravel
Maxim Zolotoy, 2019-05-08 22:49:25

How to display queries to the database on the screen in laravel?

I watch lessons and there at the teacher the dump () function issues queries to the database on the screen. But I only have data. Can you please tell me how to enable this feature?
5cd332c468ef9325000219.jpeg

Answer the question

In order to leave comments, you need to log in

4 answer(s)
J
jazzus, 2019-05-09
@jazzus

I check with Laravel Debugbar

M
Mazino, 2019-05-09
@Mazino

Knee variant
For specific request

<?php

DB::enableQueryLog();

//Ваш код .....

$dump = DB::getQueryLog();
$bindings = $dump[0]['bindings'];
$query = $dump[0]['query'];
$query = str_replace('?','"?"',$query);
dump(str_replace_array('?',$bindings,$query));

For every request
//AppServiceProvider.php
<?php

//....

public function boot() {
    DB::listen(function($query) {
         $bindings = $query->bindings;
         $sql = $query->sql;
         $sql = str_replace('?','"?"',$sql);
         dump(str_replace_array( '?', $bindings, $sql)."{$query->time} sec");   
    });
}

A
Alex Wells, 2019-05-09
@Alex_Wells

put a telescope on yourself and look

J
javanub, 2019-05-09
@javanub

dd($post), this is like the last example
$post->toSql(), this is to show as a query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question