Php Laravel-查询,进行搜索

Php Laravel-查询,进行搜索,php,laravel,search,eloquent,where-clause,Php,Laravel,Search,Eloquent,Where Clause,好的,我在我的Laravel项目中进行搜索,我使用了whereBetween子句,但它不能正常工作。让我展示代码,然后再解释 这是我的SearchController搜索功能: /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request */ public function automobilipretraga(Requ

好的,我在我的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>
所以我有两个输入名为
from
to

例如,我有三个汽车帖子,第一个帖子的
价格为15
,第二个帖子的
价格为45
,第三个帖子的
价格为80
。当我在
20和50之间搜索价格时,它不起作用,它向我显示
价格为15的车贴,但当我将搜索更改为
10和30之间时,它起作用。
这就像它只计算<代码>$到
变量,而不是<代码>从
开始。
我做错了什么?

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

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

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

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

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

if($x = 33 && $y = 44){
  echo $x; // output 33
}
但不能在if语句下将变量定义为字符串,它会给出布尔值结果,如下所示:

if($x = "33" && $y = "44"){
  echo $x; // output 1
}
在这里,当您通过请求传递变量时,它会给您一个
字符串,而不是
整数

您的代码如下所示:

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);
}

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

if($x = 33 && $y = 44){
  echo $x; // output 33
}
但不能在if语句下将变量定义为字符串,它会给出布尔值结果,如下所示:

if($x = "33" && $y = "44"){
  echo $x; // output 1
}
在这里,当您通过请求传递变量时,它会给您一个
字符串,而不是
整数

您的代码如下所示:

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);
}

我看不出您使用
$request
有任何问题。您确定在实际代码中没有双
==
或类似的内容吗


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

/**
*更新存储中的指定资源。
*
*@param\light\Http\Request$Request
*/
公共功能Automobilipretaga(请求$Request)
{
$automobili=DB::table('automobils')
->当($request->input('from')时,函数($query,$from){
$query->where('price','>=',$from);
})
->当($request->input('to')时,函数($query,$to){

$query->where('price',”我看不出您使用
$request
有任何错误。您确定在实际代码中没有双
==
或类似的东西吗


除此之外,我建议对控制器方法使用更优雅的表单。它还允许只使用一个输入,
(如果您希望允许用户搜索):

/**
*更新存储中的指定资源。
*
*@param\light\Http\Request$Request
*/
公共功能Automobilipretaga(请求$Request)
{
$automobili=DB::table('automobils')
->当($request->input('from')时,函数($query,$from){
$query->where('price','>=',$from);
})
->当($request->input('to')时,函数($query,$to){

$query->where('price',我就是这样做的,
$cijenaod=(int)$request->input('cijenaod')&&$cijenado=(int)$request->input('cijenado')
,但它仍然是一个问题,如果在代码中用数字替换$from和$to变量,$autoQuery->whereBetween('price',[20,80]);这有一个问题,因为我的变量
$from和$to
保持为空,并且它会返回我的每一个汽车贴子。我就是这样做的
$cijenaod=(int)$request->input('cijenaod')&&$cijenado=(int)$request->input('cijenado'))
但它仍然是一个问题如果在代码中用数字替换$from和$to变量会发生什么,$autoQuery->whereBetween('price',[20,80]);这是一个问题,因为我的变量
$from和$to
保持为空,并且它会返回我所有的汽车贴子这将起作用?
$autoQuery->whereBetween('price',[$request->from,$request->to]);
非常感谢,我认为它现在工作得很好!让我快速检查一下。为什么会这样,你能解释一下吗?如果($from=$request->input('from')&$to=$request->input('to'),这里的问题是
如果($from=$request->input('from')&$to=$request->input('to'))
我该怎么处理那行代码呢?只要删除它,让它像
如果{$autoQuery->whereBetween('price',[$request->from,$request->to];}
?我回答了你的问题,从中我学到了一些特别的东西,谢谢你这会有用的?
$autoQuery->whereBetween('price',[$request->from,$request->from,$request->to])非常感谢,我认为它现在工作得很好!让我快速检查一下。为什么这样工作,你能解释一下吗?这里的问题是如果($from=$request->input('from')&&&$to=$request->input('to'))
我该如何处理那行代码?只要删除它,然后像
if一样保留它就行了{$autoQuery->whereBetween('price',[$request->from,$request->to]);
?我回答了你的问题,从中我学到了一些特别的东西,谢谢你我使用
(float)
,因为将
字符串
转换为
数字
值更好,你也可以使用
(int)
(float)
给你一个十进制值,其中
(int)
给你一个没有十进制的数字。你可以忽略
(float)