【问题标题】:Call to a member function toSql() on integer在整数上调用成员函数 toSql()
【发布时间】:2017-12-25 17:44:57
【问题描述】:

我有一个代码:

$update= DB::table('Appraiser_commands')->where('cycle_id', $cycleid)->where('user_id',$userId)->update(['mode' => $mode,'Appraiser_commnd'=>$json])->toSql();
        echo $update;exit;

我正在尝试使用 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));

但它以“假”的形式返回输出,我无法得到预期的输出。我不知道为什么会这样。任何帮助将不胜感激。

预期输出:

UPDATE table_name
SET Appraiser_commnd=value, mode=value2,...
WHERE cycle_id=some_value 

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

执行以下操作:

a)在函数的开始处:

DB::enableQueryLog();

b)执行查询后执行以下操作:

dd(DB::getQueryLog());

它将给出整个查询

【讨论】:

    【解决方案2】:
        DB::enableQueryLog();    
    $update= DB::table('Appraiser_commands')->where('cycle_id', $cycleid)->where('user_id',$userId)->update(['mode' => $mode,'Appraiser_commnd'=>$json])
    
        $queries = DB::getQueryLog();
        dd(end($queries)); // only last query
    

    删除 toSql 试试这个

    【讨论】:

    • :它只返回false
    • 表示查询有错误,请尝试查找
    • @Exprator-我知道有一个错误。这就是为什么我试图将其转换为 sql.so 我可以轻松找到错误
    • 我不知道是否打印了错误的查询,请尝试不结束并检查是否获得所有查询
    【解决方案3】:

    update 的返回类型是 int,toSql() 需要一个 Builder 实例。

    如果在 where 子句中使用 AND,您可以将查询更改为以下内容并尝试将其 sql 转储为

    $update = DB::table('Appraiser_commands')
                ->where(
                    ['cycle_id', '=', $cycleid],
                    ['user_id', '=', $userId]
                )->update([
                    'mode' => $mode,
                    'Appraiser_commnd' => $json
                ]);
    

    如果在 where 子句中使用 OR,您可以尝试 as

    $update = DB::table('Appraiser_commands')
                ->where('cycle_id', $cycleid)
                ->orWhere('user_id', $userId)
                )->update([
                    'mode' => $mode,
                    'Appraiser_commnd' => $json
                ]);
    

    执行查询后

    $queries = DB::getQueryLog();
    dd($queries);
    

    【讨论】:

      【解决方案4】:

      你可以不获取 原始查询 用于 Laravel 查询生成器 和->toSql() 中的更新查询仅适用于选择查询。
      但是,如果您想获取在 laravel 中执行的先前查询,则不要遵循此

      How to get the query executed in Laravel 5 ? DB::getQueryLog returning empty array

      【讨论】:

        【解决方案5】:

        当您调用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]);
        

        如果你确实想执行查询,但也想得到 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]);
        

        另外,您可以在查询日志中查看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());
        

        【讨论】:

        • @JeffPuckett 你完全正确。我已经更新了提供的代码以传入构建器对象。感谢您指出。
        猜你喜欢
        • 2020-03-16
        • 2016-08-21
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 2017-08-09
        • 1970-01-01
        相关资源
        最近更新 更多