Toma Rareş
Toma Rareş

Reputation: 63

{{Input::get('min_price')}} is not working. How to fix this?

I am a beginner in Laravel, and I am trying to search between two values (min_price and max_price). Everything is working well recording to the search, but I cannot show the selected price in my inputs after each search.

I am running Laravel framework 5.8.27. I tried getting data with {{Input::get('min_price')}} but it is not working.

AdController (where I make the function that browses between the two values)

public function browse()
{
    $ads = Ad::where(function ($query) {
        $min_price = Input::has('min_price') ? Input::get('min_price') : null;
        $max_price = Input::has('max_price') ? Input::get('max_price') : null;

        if (isset($min_price) && isset($max_price)) {
            $query->where('rent', '>=', $min_price)
                ->where('rent', '<=', $max_price);

        }
    })->get();

    return view('browse', compact('ads'));
}

web.php

Route::get('/browse', 'AdController@browse');

browse.blade.php

<form action="{{ URL::current() }}">
    <div>
        <label for="">Price Range</label>
        min <input type="text" name="min_price" value="{{ Input::get('min_price') }}">
        max <input type="text" name="max_price">
    </div>
    <button>ok</button>
</form>

I expect to have in my input file after search the value I entered(for example 200), but it throws me the error:

Class 'Input' not found (View: /Users/tomarares/Developer/roombuddy/resources/views/browse.blade.php)

Upvotes: 0

Views: 126

Answers (1)

NewWorldNeverland
NewWorldNeverland

Reputation: 308

To retrieve the min and max price variables, you can do the following:

In Controller

public function browse()
{
    $min_price = Input::has('min_price') ? Input::get('min_price') : null;
    $max_price = Input::has('max_price') ? Input::get('max_price') : null;

    $ads = Ad::where(function($query) use($min_price, $max_price){
        if(!is_null($min_price) && !is_null($max_price)){
            $query-> where('rent', '>=', $min_price)
            ->where('rent','<=',$max_price);
        }
    })->get();

    return view('browse', compact('ads', 'min_price', 'max_price'));
}

In View

<div>
    <label for="">Price Range</label>
    min <input type="text" name="min_price" value="{{ old('min_price', $min_price) }}">
    max <input type="text" name="max_price" value="{{ old('max_price', $max_price) }}">
</div>

Upvotes: 1

Related Questions