sby05922
sby05922

Reputation: 35

Get raw sql query from belongsToMany relationship

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 protected]","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}}]

Upvotes: 0

Views: 1486

Answers (1)

Aubin
Aubin

Reputation: 87

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

Upvotes: 1

Related Questions