代码之家  ›  专栏  ›  技术社区  ›  Константин Ковалев

Laravel eloquent selectRaw for SQL注入

  •  0
  • Константин Ковалев  · 技术社区  · 7 年前

    我有疑问:

    $typeCancel = 'cancel'; $typeRefund = 'refund';
    $query = Order::select(DB:raw('order.*,'
        "IF (order.type IN (?, ?), (order.amount * -1), order.amount) AS custom_amount"))
    ->where('provider', 1)
    ->setBindings([$typeCancel, $typeRefund], 'select')
    ->get();
    

    这很有效。 我该如何做:

    ... IF (order.type IN (?) ...
    ... ->setBindings([$types], 'select')
    

    我尝试以下方法:

    $types = "'{$typeCancel}','{$typeRefund}'";
    ... IF (order.type IN (?) ...
    ... ->setBindings([$types], 'select')
    

    但它不正确,结果查询如下所示:

    ... IF (order.type IN ("'cancel','refund'") ...
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   adkstar    7 年前

    你可以做到 $types = array($typeCancel, $typeRefund); 然后你可以通过 $types 到您的绑定