【问题标题】:Laravel - query, making a searchLaravel - 查询,进行搜索
【发布时间】:2020-07-05 14:12:32
【问题描述】:

好的,我正在我的 Laravel 项目中进行搜索,我正在使用 whereBetween 子句,但它无法正常工作。让我展示代码而不是解释。

这是我的SearchController 搜索功能:

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     */
    public function automobilipretraga(Request $request)
    {
        $autoQuery = DB::table('automobils');

      
        if (
            $from = $request->input('from')
            && $to = $request->input('to')
        ) {
            $autoQuery->whereBetween('price', [$from, $to]);
        }

        //vracanje rezultata
        $automobili = $autoQuery->get();

        return view('site.automobili.pretraga')->with('automobili', $automobili);
    }

在我的search.blade.php:

<div id="collapsecijena" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="cijena">
   <div class="panel-body">
      <div class="cs-model-year">
         <div class="cs-select-filed">
            <input type="text" name="from">
         </div>
         <span style="color:black;">TO</span>
         <div class="cs-select-filed">
            <input type="text" name="to">
         </div>
      </div>
   </div>
</div>

所以我将这两个输入命名为 fromto

例如,我有三个汽车帖子,第一个有一个price of 15,第二个有一个price of 45,第三个有一个price of 80。当我在20 and 50 之间搜索价格时它不起作用,它会显示带有price of 15 的汽车帖子,但是当我将搜索更改为10 and 30 之间时它可以工作。 就像它只计算$to 变量而不是from。 我做错了什么?

【问题讨论】:

  • 这行得通吗? $autoQuery-&gt;whereBetween('price', [$request-&gt;from, $request-&gt;to]);
  • 非常感谢,我认为它现在可以完美运行了!让我快速检查一下。为什么这是有效的,你能解释一下吗?
  • 这里有问题if ( $from = $request-&gt;input('from') &amp;&amp; $to = $request-&gt;input('to') )
  • 我应该如何处理那行代码?只需将其删除并保留为if { $autoQuery-&gt;whereBetween('price', [$request-&gt;from, $request-&gt;to]); } ?
  • 我回答了你的问题,我从中学到了一些特别的东西,谢谢

标签: php laravel search eloquent where-clause


【解决方案1】:

问题出在 if else 语句
您可以在 if 语句中将变量定义为 integer

if($x = 33 && $y = 44){
  echo $x; // output 33
}

但是您不能在 if 语句下将变量定义为 string,它会为您提供 boolean 结果,如下所示:

if($x = "33" && $y = "44"){
  echo $x; // output 1
}

当您通过 Request 传递变量时,它会为您提供 string,而不是 integer

您的代码将是这样的:

public function automobilipretraga(Request $request)
{
    $autoQuery = DB::table('automobils');

    if (
       $from = $request->input('from')
       && $to = $request->input('to')
       ) {
           $autoQuery->whereBetween('price', [(float)$from = $request->input('from'), (float)$to = $request->input('to')]); // float will convert this string as float value like 10.00 
         }

     //vracanje rezultata
     $automobili = $autoQuery->get();

    return view('site.automobili.pretraga')->with('automobili', $automobili);
}

【讨论】:

  • 我使用(float),因为将string 转换为number 值更好,您也可以使用(int)(float) 给你一个十进制值,(int) 给你一个没有小数的数字。你也可以忽略(float)
【解决方案2】:

您是否尝试过将输入转换为整数

$from = (int)$from
$to = (int)$to

【讨论】:

  • 我只是喜欢这个$cijenaod = (int)$request-&gt;input('cijenaod') &amp;&amp; $cijenado = (int)$request-&gt;input('cijenado'),但它仍然是同一个问题
  • 如果在代码中仅用数字替换 $from 和 $to 变量会发生什么情况,$autoQuery->whereBetween('price', [20, 80]);
  • 这有问题,因为我的变量$from and $to 保持为空,而且每辆汽车的帖子都会返回给我
【解决方案3】:

我认为您对$request 的使用没有任何问题。您确定在您的实际代码中的if 中没有双重== 或类似的东西吗?


除此之外,我建议为您的控制器方法使用更优雅的形式。它还允许只使用一个输入,fromto(如果您想让用户搜索):

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 */
public function automobilipretraga(Request $request)
{
    $automobili = DB::table('automobils')
        ->when($request->input('from'), function ($query, $from) {
            $query->where('price', '>=', $from);
        })
        ->when($request->input('to'), function ($query, $to) {
            $query->where('price', '<=', $to);
        })
        ->get();

    return view('site.automobili.pretraga')->with('automobili', $automobili);
}

请注意:when($condition, $callback) 方法将$condition 的结果作为第二个参数传递给$callback 函数。如果您输入0 作为$request-&gt;input('from') 的值,$condition 的计算结果为false,并且不会添加查询约束。在这种情况下,您需要将$request-&gt;input('from') !== null 用作$condition,这需要您在回调中显式访问$request-&gt;input('from')。不要忘记在回调中添加use 子句:function ($query, $condition) use ($request) { ... }

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 2017-05-15
    • 2017-03-04
    • 2021-08-23
    • 2018-10-21
    • 2016-07-25
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多