Laravel 5:whereRaw将整数转义为字符串

时间:2016-11-20 19:26:47

标签: mysql laravel-5 eloquent

Laravel雄辩的问题是什么:

public static void main(String[] args) {

    String fileName = "Cowlic105.txt";
    String fileName2 = "FormDatFile.txt";
    String line = null;
    int x;
    int gageID;
    int precipYTD;



    try {
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {

            System.out.println(line);
        }

        bufferedReader.close();
    }

    catch(FileNotFoundException ex) {

        System.out.println("Unable to open file '" + fileName + "'");
    }


    catch(IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }

    Scanner in = new Scanner(System.in);
    System.out.println("Is this the file you wish to format? " + fileName + "(0 for no, 1 for yes)");
    x = in.nextInt();

    if (x == 0){

        System.exit(0);

    }
    else if (x == 1){
        System.out.println("Hello there you resumed the program!!!");

        System.out.println("What is the ID of the rain gage? ");
        gageID = in.nextInt();

        System.out.println("What is the value of precipitation YTD? ");
        precipYTD = in.nextInt();

        File destFile =  new File("FormDatFile.txt");

        if(!destFile.exists()){
            try {
                destFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        try {

            FileInputStream fis = new FileInputStream(destFile);


            FileWriter writer = new FileWriter(destFile, true);
            BufferedWriter write2 = new BufferedWriter(writer);

            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));

            while((line = bufferedReader.readLine()) != null){
                write2.write(line);
                write2.newLine();

                bufferedReader.close();
                write2.close();
            }




            writer.write("Gage ID,Date/Time,Precip_1min,Precip/Year");
            writer.write("\r\n");                
            writer.close();

        }
        catch (IOException e){
            e.printStackTrace();
        }
    }






}

我尝试了以下雄辩的查询,它将整数封装成字符串

select * from `jobs` where (
      400000 between min_salary and max_salary
            or
      600000 between min_salary and max_salary
 );

还尝试了Casting和DB :: Raw,没有一个选项按预期工作。

$min = 400000;
$max = 600000;
Job::whereRaw('
     ? between min_salary and max_salary
        or
     ? between min_salary and max_salary',
     [$min,$max]
)->get();

我尝试了以下雄辩查询按预期工作,但我直接硬编码查询(不安全)

protected $casts = [
    'min_salary' => 'integer',
    'max_salary' => 'integer',
];

$min = 400000;
$max = 600000;
Job::whereRaw('
     ? between min_salary and max_salary
        or
     ? between min_salary and max_salary',
     [DB::Raw($min),DB::Raw($max)]
)->get();

1 个答案:

答案 0 :(得分:3)

试试这个:

Job::where(function($query){
    $query->where('min_salary','<',400000);
    $query->where('max_salary','>',400000);
})->orWhere(function($query){
    $query->where('min_salary','<',600000);
    $query->where('max_salary','>',600000);
})->get();