我有一个代码:
$update= DB::table('Appraiser_commands')->where('cycle_id', $cycleid)->where('user_id',$userId)->update(['mode' => $mode,'Appraiser_commnd'=>$json])->toSql();
echo $update;exit;
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用toSql()将laravel查询转换为mysql
但是我有一个错误
在整数上调用成员函数toSql()
然后我尝试
DB::table('Appraiser_commands')->where('cycle_id', $cycleid)->where('user_id',$userId)->update(['mode' => $mode,'Appraiser_commnd'=>$json])
DB::enableQueryLog();
$queries = DB::getQueryLog();
dd(end($queries));
Run Code Online (Sandbox Code Playgroud)
但是它返回的输出为'false'我没有得到预期的输出。我不知道为什么会这样,任何帮助将不胜感激。
预期的输出:
UPDATE table_name
SET Appraiser_commnd=value, mode=value2,...
WHERE cycle_id=some_value
Run Code Online (Sandbox Code Playgroud)
调用时update(),它将执行查询并返回受影响的行数(整数)。然后,您调用toSql()整数。这将导致您看到的错误。
如果试图在不运行查询的情况下查看SQL,则需要执行以下操作:
// start the query but don't execute it
$query = DB::table('Appraiser_commands')
->where('cycle_id', $cycleid)
->where('user_id', $userId);
// use the grammar object to get the update sql
$sql = $query
->getGrammar()
->compileUpdate($query, ['mode' => $mode, 'Appraiser_commnd' => $json]);
Run Code Online (Sandbox Code Playgroud)
如果确实要执行查询,但又要获取SQL,则可以执行以下操作:
// start the query but don't execute it
$query = DB::table('Appraiser_commands')
->where('cycle_id', $cycleid)
->where('user_id', $userId);
// use the grammar object to get the update sql
$sql = $query
->getGrammar()
->compileUpdate($query, ['mode' => $mode, 'Appraiser_commnd' => $json]);
// actually run the update statement
$updated = $query
->update(['mode' => $mode, 'Appraiser_commnd' => $json]);
Run Code Online (Sandbox Code Playgroud)
此外,您可以在查询日志中查看sql,但是在执行该语句之前必须启用查询日志:
// enable the query log
DB::enableQueryLog();
// execute the update
DB::table('Appraiser_commands')
->where('cycle_id', $cycleid)
->where('user_id', $userId)
->update(['mode' => $mode, 'Appraiser_commnd' => $json]);
// view the query log
dd(DB::getQueryLog());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4059 次 |
| 最近记录: |