在Laravel中的搜索表单中定义最小价格到最大价格的问题

时间:2019-07-03 10:31:21

标签: php laravel

我有搜索表单,可以通过某些条件列出属性/广告。在我的表单中,我试图从数据库中搜索并列出与输入的min_price和max_price相对应的属性。当我提交表单时,表中没有任何结果,当我死掉并转储min_price或max_price变量时,我得到的是假。任何帮助表示赞赏。这是我的代码

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>

3 个答案:

答案 0 :(得分:2)

主要问题是因为您在$request中没有数据,但是在变量$price中却有这样的东西:"min_price=1000-max_price=2000"

因此您需要从该字符串获取值:

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

也许您需要尝试以更好的格式发送min_pricemax_price,但这应该可行。

祝你好运。

答案 1 :(得分:1)

您可以使用laravel elqouent when方法执行此操作。

因此您的查询应类似于以下内容

$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();

有关更多详细信息,请查看此https://laraveldaily.com/less-know-way-conditional-queries/

祝你好运

答案 2 :(得分:0)

$request->has是一个laravel函数,用于检查您的请求是否具有特定值,它将始终返回true或false。在获取请求值时,可以使用$request->get('name'),因此您的搜索函数应为:

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

}