doudang9147 2015-07-19 17:39
浏览 61

Laravel 5中的原始SQL

I've got this code in my controller;

$sql = "SELECT * FROM `contracts`.`payments` as t1 
LEFT JOIN `postcodes`.`" . $postcode_lookup->dataset . "` AS t2 ON t1.`VendorZIP` = t2.`postcode` WHERE "; 

foreach ($input as $key => $value) {
    $sql .= "t2." . $key . " IN ('".implode("', '", $input[$key])."') OR ";
}

$sql = rtrim($sql, " OR ");

Which will return a working statement (when ran in SequelPro or PHPMyAdmin for example);

SELECT * FROM `contracts`.`payments` as t1 
LEFT JOIN `postcodes`.`201502_postcode` AS t2 ON t1.`VendorZIP` = t2.`postcode` 
WHERE t2.country IN ('L93000001', 'E92000001', 'M83000003', 'S92000003') 
OR t2.county IN ('E10000002') 
OR t2.gor IN ('Z', 'E', 'G', 'H') 
OR t2.locauth IN ('S12000033') 
OR t2.parlc IN ('W07000049')

How can I use raw queries in Laravel to run this in my application? and $payments = DB::raw($sql); doesn't do anything.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比