【问题标题】:{{Input::get('min_price')}} is not working. How to fix this?{{Input::get('min_price')}} 不起作用。如何解决这个问题?
【发布时间】:2019-07-21 08:31:42
【问题描述】:

我是 Laravel 的初学者,我正在尝试在两个值(min_price 和 max_price)之间进行搜索。一切都很好地记录到搜索中,但我无法在每次搜索后在我的输入中显示所选价格。

我正在运行 Laravel 框架 5.8.27。我尝试使用{{Input::get('min_price')}} 获取数据,但它不起作用。

AdController(我在这里创建了在两个值之间浏览的函数)

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>

我希望在搜索我输入的值(例如 200)后在我的输入文件中有,但它会抛出错误:

找不到类“输入”(查看: /Users/tomarares/Developer/roombuddy/resources/views/browse.blade.php)

【问题讨论】:

    标签: laravel


    【解决方案1】:

    要检索最低和最高价格变量,您可以执行以下操作:

    在控制器中

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

    可见

    <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>
    

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多