Generate The Raw MySQL Query From Laravel Query Builder

后端 未结 14 1179
醉话见心
醉话见心 2021-02-02 12:04

How can i get mysql query of a laravel query

Convert:

App\\User::where(\'balance\',\'>\',0)->where(...)-         


        
14条回答
  •  日久生厌
    2021-02-02 12:56

    you can add this function to your helpers

    function getRealQuery($query, $dumpIt = false)
    {
        $params = array_map(function ($item) {
            return "'{$item}'";
        }, $query->getBindings());
        $result = str_replace_array('\?', $params, $query->toSql());
        if ($dumpIt) {
            dd($result);
        }
        return $result;
    }
    

    and use like this:

    getRealQuery(App\User::where('balance','>',0)->where(...),true)
    

提交回复
热议问题