mrmar
mrmar

Reputation: 803

Problem with defining filter minimum to maximum price in search form in Laravel

I have search form to list properties/ads through certain criteria. In my form I am trying to search and list properties from database that correspond to entered min_price and max_price. When I submit form I get no results in table and when I die and dump min_price or max_price variable I get false. Any help is appreciated. Here is my code

CategoryController.php

<?php
namespace App\Http\Controllers;

use App\Category;
use App\Property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

class CategoryController extends Controller
{
    public function index()
    {
        return view('categories.search', compact('data'));
    }

    public function search($price, Request $request, Property $property)
    {
        $category = $property->category;

        $query = Property::query();

        // Code for min and max price

        $min_price = $request->has('min_price');
        $max_price = $request->has('max_price');
        //dd($max_price);

        if (($min_price) && ($max_price)) {
            $query->whereBetween('price', [$min_price, $max_price]);
        }
        elseif (! is_null($min_price)) {
            $query->where('price', '>=', $min_price);
        }
        elseif (! is_null($max_price)) {
            $query->where('price', '<=', $max_price);
        }

        $results = $query->get();

        return view('categories.search', compact('category', 'results'));
    }
}

search.blade.php

@if(isset($results))
    <table class="table">
        <thead>
            <th>Price</th>
        </thead>
        <tbody>
            @foreach ($results as $result)
                <tr>
                    <td>{{ $result->price }}</td>
                </tr>
            @endforeach
        </tbody>
     </table>
@endif

<form id="searchForm" method="GET" action="/search">

    <div class="col-md-6 mb-6">
        <label>Price</label>
        <input type="number" id="min_price" name="min_price" class="form-control" placeholder="Min Price">
        <input type="number" id="max_price" name="max_price" class="form-control" placeholder="Max Price">
    </div>
    <button class="btn btn-primary btn-lg btn-block">Search</button>

</form>

Upvotes: 2

Views: 4554

Answers (3)

mare96
mare96

Reputation: 3859

Main problem was is because you didn't have data in $request but in your variable $price you had something like this: "min_price=1000-max_price=2000".

So you need to get values from that string:

public function search($price, Request $request, Property $property)
    {
        $category = $property->category;
        $array = explode('-', $price); // Array of your values
        foreach($array as $a){ 
            $s = []; 
            $s = explode('=', $a); 
            if ($s[0] === 'min_price'){ 
               $s[1] ? $min_price = intval($s[1]) : $min_price = null;
            } else { 
               $s[1] ? $max_price = intval($s[1]) : $max_price = null; 
            } 
        }
        if (!empty($min_price) && !empty($max_price)) {
            $results = Property::whereBetween('price', [$min_price, $max_price])->get();

            return view('categories.search', compact('category', 'results'));
        }
        elseif (!empty($min_price)) {
            $results = Property::where('price', '>=', $min_price)->get();
        }
        elseif (!empty($max_price)) {
            $results = Property::where('price', '<=', $max_price)->get();
        }

        return view('categories.search', compact('category', 'results'));
    }

Maybe you need to try to send min_price and max_price in better format, but this should work.

Good luck.

Upvotes: 2

usrNotFound
usrNotFound

Reputation: 2810

You can use laravel elqouent when method do do this.

So your query should look something like the following

$query = Property::query();


$request->has('max_price') && $request->has('min_price')
? $query->whereBetween('price', [$request->get('min_price'), $request->get('max_price')])
: $query->when($request->has('min_price'), function ($query) use ($request) {
$query->where('price', '>=', $request->get('min_price'));
})->when($request->has('min_price'), function ($query) use ($request) {
$query->where('price', '<=', $request->get('max_price'));
});

$results = $query->get();

Have a look at this https://laraveldaily.com/less-know-way-conditional-queries/ for more details

Good Luck

Upvotes: 1

Muhammad Bilal
Muhammad Bilal

Reputation: 517

$request->has is a laravel function just to check if your request has certain value or not it will always return true or false.While to get request value you can use $request->get('name') so your search function should be:

public function search($price, Request $request, Property $property)
{
    $category = $property->category;

    $query = Property::query();

    // Code for min and max price
    $min_price= 0;
    $max_price= 0;
    if($request->has('min_price')){
        $min_price = $request->get('min_price');
    }

    if($request->has('max_price')){
        $max_price = $request->get('max_price');
    }

    //dd($max_price);

    if (($min_price) && ($max_price)) {
        $query->whereBetween('price', [$min_price, $max_price]);
    }
    elseif (! is_null($min_price)) {
        $query->where('price', '>=', $min_price);
    }
    elseif (! is_null($max_price)) {
        $query->where('price', '<=', $max_price);
    }

    $results = $query->get();

    return view('categories.search', compact('category', 'results'));
}

}

Upvotes: 0

Related Questions