Milos
Milos

Reputation: 602

Laravel - query, making a search

Okay I'm making a search in my Laravel project and I'm using whereBetween clause, but it's not working properly. Let me show the code than I'll explain.

So this is my SearchController function for search:

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

And in my 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>

So I have these two inputs named from and to.

For example, I have three cars posts and first have a price of 15, second one have a price of 45 and a third one have a price of 80. When I search price between 20 and 50 it doesn't work, it shows me car post with price of 15, but when I change my search to between 10 and 30 it works. It's like it only counts $to variable and not from. What I'm doing wrong?

Upvotes: 1

Views: 661

Answers (3)

Namoshek
Namoshek

Reputation: 6544

I don't see anything wrong with your usage of $request. You sure there is no double == or something like that in the if in your actual code?


Besides that, I recommend using a more elegant form for your controller method. It also allows using only one input, from or to (if that's something you want to allow your users to search for):

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

Please note: The when($condition, $callback) method passes the result of $condition as second argument to the $callback function. If you were to enter 0 as value for $request->input('from'), $condition would evaluate to false and the query constraint would not be added. In such case you therefore need to use $request->input('from') !== null as $condition, which requires you to access $request->input('from') explicitely in the callback. Don't forget to add the use clause on the callback though:function ($query, $condition) use ($request) { ... }

Upvotes: 0

STA
STA

Reputation: 34688

The problem is on the if else statement
You can define a variable as integer on if statement :

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

But you can't define a variable as string under if statement, it gives you the boolean result, as like :

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

Here when you passed a variable by Request, it gives you an string, not integer.

Your code will be like this :

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

Upvotes: 3

Felix Ekl&#246;f
Felix Ekl&#246;f

Reputation: 3720

Have you tried converting input to an integer

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

Upvotes: 0

Related Questions