0

How do I get raw sql like "SELECT * FROM table WHERE id = 1" from belongsToMany.

public function users(){
    return $this->belongsToMany('App\User','user_projects','project_id','user_id');
}

i tryed dd()


tryed this:

$query = $this->belongsToMany('App\User','user_projects','project_id','user_id');
        dd($query->getQuery()->toRawSql(), $query->getBindings());

Output:

Users [{"id":2,"name":"My Name","email":"email@example.com","email_verified_at":null,"currant_workspace":1,"avatar":null,"created_at":"2019-08-31 04:35:20","updated_at":"2019-08-31 04:37:26","pivot":{"workspace_id":3,"user_id":2}}]
sby05922
  • 35
  • 6
  • Temporarily get rid of the return and try `dd($this->belongsToMany(...)->toSql());` and see if you can view it there. Alternatively, look into enabling the query log in Laravel and view the queries as they are being run. – Tim Lewis Sep 16 '19 at 21:13
  • @TimLewis It prints data of table and pivot.. – sby05922 Sep 16 '19 at 21:30
  • Possible duplicate of [How Do I Get the Query Builder to Output Its Raw SQL Query as a String?](https://stackoverflow.com/questions/18236294/how-do-i-get-the-query-builder-to-output-its-raw-sql-query-as-a-string) – Isaac Gomes Sep 17 '19 at 00:21

1 Answers1

1

I don't think you can print the query.

The belongsToMany() functions are done after the initial request. You may debug the queries you could do with the debugBar plug (https://github.com/barryvdh/laravel-debugbar)

Hope this help

Aubin
  • 87
  • 1
  • 5