0

In User model I define relation :

\App\User::where('id' ,1)->toSql();

it return select * from users where id = ? how can I get full query including params?

Also I define relation with post model:

public function posts(){
    return $this->hasMany(\App\Posts::class);
}

How to view full relational query with params?

Paritosh Mahale
  • 1,238
  • 2
  • 14
  • 42

1 Answers1

0

There are two ways to go about it: the Laravel way, and the MySQL way.

The Laravel way is to use the DB::getQueryLog();

\App\User::where('id' ,1)->get();
$queries = \DB::getQueryLog();
Log::info(end($queries)); // OR
dd(end($queries));

The MySQL way involves the general log.

mysql> show variables like 'general_log%';
+------------------+------------------------------+
| Variable_name    | Value                        |
+------------------+------------------------------+
| general_log      | OFF                          |
| general_log_file | /var/lib/mysql/homestead.log |
+------------------+------------------------------+

Check your variables for the location of the log. Before the query, run SET GLOBAL general_log = 1, and make sure to set it back to 0 after the query is done. Then open up that log file, and you'll see both the Prepare and Execute lines.

aynber
  • 22,380
  • 8
  • 50
  • 63