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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.