代码之家  ›  专栏  ›  技术社区  ›  Shanu k k

对整数调用成员函数toSql()

  •  2
  • Shanu k k  · 技术社区  · 9 年前

    我有一个代码:

    $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));
    

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

    UPDATE table_name
    SET Appraiser_commnd=value, mode=value2,...
    WHERE cycle_id=some_value 
    
    5 回复  |  直到 9 年前
        1
  •  5
  •   patricus    9 年前

    当你打电话的时候 update() toSql() 在整数上。这导致了您看到的错误。

    // 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]);
    

    // 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());
    
        2
  •  3
  •   Aditya Singh    9 年前

    执行以下操作:

    a) 在功能开始时:

    DB::enableQueryLog();
    

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

    dd(DB::getQueryLog());
    

        3
  •  1
  •   Bibhudatta Sahoo    9 年前

    你可以 无法获取 这个 更新查询 和 ->toSql() 只为 选择查询 .

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

        4
  •  1
  •   linktoahref Nimya V    9 年前

    返回类型 update 是 int 和 toSql() 需要生成器实例。

    如果使用 AND

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

    OR 在where子句中,您可以尝试

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

    执行查询后

    $queries = DB::getQueryLog();
    dd($queries);
    
        5
  •  1
  •   Exprator    9 年前
        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