从belongsToMany关系获取原始SQL查询

时间:2019-09-16 21:08:53

标签: laravel laravel-5 eloquent eloquent--relationship

如何从belongsToMany获取原始SQL,例如“ SELECT * FROM table WHERE id = 1”。

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

1 个答案:

答案 0 :(得分:1)

我认为您无法打印查询。

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

希望获得帮助