Does Laravel's toSql() method mask ids? (column value being replaced by question mark)

前端 未结 4 1065
野的像风
野的像风 2020-12-15 23:46

I\'m trying to debug some SQL queries that I\'m doing in a testing suite. Using the following debugging code:

\\Log::debug(User::first()->jobs()->toSql         


        
4条回答
  •  温柔的废话
    2020-12-16 00:02

    Laravel uses Prepared Statements. They're a way of writing an SQL statement without dropping variables directly into the SQL string. The ? you see are placeholders or bindings for the information which will later be substituted and automatically sanitised by PDO. See the PHP docs for more information on prepared statements http://php.net/manual/en/pdo.prepared-statements.php

    To view the data that will be substituted into the query string you can call the getBindings() function on the query as below.

    $query = User::first()->jobs();
    
    dd($query->toSql(), $query->getBindings());
    

    The array of bindings get substituted in the same order the ? appear in the SQL statement.

提交回复
热议问题