返回带有whereBetween的行

时间:2019-03-10 04:14:15

标签: laravel

如何根据输入的参数提取一行,然后将数据传递给视图?

我有此代码,但它不起作用:

$taxableBase = $request->taxableBase;

$lowerL_and_upperL = DB::table('table_taxes')->whereBetween($taxableBase, ['lower_limit', 'upper_limit'])->get();

    return view('calculator.taxes', compact('lowerL_and_upperL'));
php中的

以这种方式可以正常工作:

$lowerL_and_upperL = $conexion->prepare('SELECT * FROM table_taxes WHERE ? BETWEEN lower_limit AND upper_limit');
    $lowerL_and_upperL->execute([$taxableBase]);

    while ($row = $lowerL_and_upperL->fetch()) {
        $lower_limit   = $row['lower_limit'];
        $upper_limit   = $row['upper_limit'];

    }

2 个答案:

答案 0 :(得分:0)

您没有给出两个变量或限制。您刚刚给了两个字符串数组

$taxableBase = $request->taxableBase;

$lowerL_and_upperL = DB::table('table_taxes')->whereBetween($taxableBase, [$lower_limit, $upper_limit])->get();

return view('calculator.taxes', compact('lowerL_and_upperL'));

答案 1 :(得分:0)

制作where()子句。

$taxableBase = $request->taxableBase;

$lowerL_and_upperL = DB::table('table_taxes')->where(function($q) use ($taxableBase){
                                  $q->where('lower_limit', '<=', $taxableBase);
                                  $q->where('upper_limit', '>=', $taxableBase);
                     })->get();

return view('calculator.taxes', compact('lowerL_and_upperL'));

为进一步查询,Here是用SO回答的另一个问题。