从 belongsToMany 关系中获取原始 sql 查询

Get raw sql query from belongsToMany relationship

如何从 belongsToMany.

中获取像 "SELECT * FROM table WHERE id = 1" 这样的原始 sql
public function users(){
    return $this->belongsToMany('App\User','user_projects','project_id','user_id');
}

我试过 dd()


试过这个:

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

输出:

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}}]

我认为您无法打印查询。

belongsToMany() 函数在初始请求后完成。您可以调试可以使用 debugBar 插件执行的查询 (https://github.com/barryvdh/laravel-debugbar)

希望对您有所帮助