php - 我在 Laravel 中的搜索表单中的过滤器问题
问题描述
我有搜索表单通过某些条件列出属性/广告。我的搜索表单有效,但是当我使用多个过滤器进行搜索时,我没有得到想要的结果。我有三种类型的过滤器。物业出价(需求,报价),物业付款(购买,租金),物业类型(房屋,公寓,车库)。例如,当我单击按需求、购买、房屋进行搜索时,它会返回一个结果,其中存在需求、购买、房屋,以及另外两个存在购买和房屋但第三个参数是报价的结果。我正在努力实现过滤器满足所有三个条件而不仅仅是两个条件。任何帮助表示赞赏。这是我的代码。
类别控制器.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($propertyBidAsk, $propertyPayment, $propertyType, $city, $price, $quadrature, Request $request, Property $property)
{
$category = $property->category;
if (!empty($request->propertyBidAsk)) {
$property = Property::whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
})->get();
}
if (!empty($request->propertyPayment)) {
$property = Property::whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyPayment . '%');
})->get();
}
if (!empty($request->propertyType)) {
$property = Property::whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyType . '%');
})->get();
}
$results = $property;
return view('categories.search', compact('category', 'results'));
}
}
search.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="py-5 text-center">
<h2>Search</h2>
</div>
<div class="row justify-content-md-center">
<div class="col-md-8 order-md-1">
<div>
@if(isset($results))
<table class="table">
<thead>
<th>Property Bid Ask</th>
<th>Property Payment</th>
<th>Property Type</th>
</thead>
<tbody>
@foreach ($results as $result)
<tr>
<td>{{ $result->category[0]->category }}</td>
<td>{{ $result->category[1]->category }}</td>
<td>{{ $result->category[2]->category }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
<form id="searchForm" method="GET" action="/search">
<div class="row">
<hr class="mb-4">
<div class="row">
<div class="col-md-4 mb-6">
<h5>Payment</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="offer" name="propertyBidAsk" value="offer" type="radio" class="custom-control-input">
<label class="custom-control-label" for="offer">offer</label>
</div>
<div class="custom-control custom-radio">
<input id="demand" name="propertyBidAsk" value="demand" type="radio" class="custom-control-input">
<label class="custom-control-label" for="demand">demand</label>
</div>
</div>
</div>
<div class="col-md-3 mb-6">
<h5>Property payment</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="buy" name="propertyPayment" value="buy" type="radio" class="custom-control-input">
<label class="custom-control-label" for="buy">buy</label>
</div>
<div class="custom-control custom-radio">
<input id="rent" name="propertyPayment" value="rent" type="radio" class="custom-control-input">
<label class="custom-control-label" for="rent">rent</label>
</div>
</div>
</div>
<div class="col-md-5 mb-6">
<h5>Property type</h4>
<div class="d-block my-3 ">
<div class="custom-control custom-radio">
<input id="house" name="propertyType" value="house" type="radio" class="custom-control-input">
<label class="custom-control-label" for="house">Kucahouse/label>
</div>
<div class="custom-control custom-radio">
<input id="flat" name="propertyType" value="flat" type="radio" class="custom-control-input">
<label class="custom-control-label" for="flat">flat</label>
<div class="custom-control custom-radio">
<input id="garage" name="propertyType" value="garage" type="radio" class="custom-control-input">
<label class="custom-control-label" for="garage">garage</label>
</div>
</div>
</div>
</div>
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block">Search</button>
</form>
<script>
var onSubmitFunc = function(e){
e.preventDefault();
e.stopPropagation();
if( e.stopImmediatePropagation ){
e.stopImmediatePropagation();
}
var propertyBidAsk = this["propertyBidAsk"].value.trim() || 0;
var propertyPayment = this["propertyPayment"].value.trim() || 0;
var propertyType = this["propertyType"].value.trim() || 0;
url = propertyBidAsk.length === 0 ? '' : ( '/' + encodeURIComponent(propertyBidAsk) );
url += propertyPayment.length === 0 ? '' : ( '/' + encodeURIComponent(propertyPayment) );
url += propertyType.length === 0 ? '' : ( '/' + encodeURIComponent(propertyType) );
window.location.href = this.action + url;
}
document.addEventListener( 'DOMContentLoaded', function(){
var srch = document.getElementById("searchForm");
srch.addEventListener('submit', onSubmitFunc, false);
}, false );
</script>
</div>
</div>
</div>
@endsection
解决方案
我认为在这种情况下,如果多个条件为真,那么您将覆盖变量$property
,那么即使您的第一个查询运行,它也会在第二个中替换所有数据。if condition
要解决此类问题,请尝试将所有三个查询存储result of ->get()
在数组中并返回该数据数组。您必须首先在顶部分配一个空数组,并且只要条件为真,就将该数据相应地存储在数组中。
推荐阅读
- php - Laravel Fortify 电子邮件验证重定向到 /home
- php - 如何将具有嵌套属性的 JSON 对象反序列化为 Symfony 实体?
- c# - 在 ClosedXML 中将列设置为文本
- replace - LoadHTML & SaveHTML 无意循环
- cakephp - cakePHP 3.10 - 当不使用“默认”数据库连接时......控制器找不到 Flash!cakePHP 正在跳过 AppController->initialize()
- google-chrome - Chrome 商店重定向到哪里?
- amazon-web-services - AWS IAM 角色,允许更新/删除自己的分配
- java - 如何制作一种方法来检查一个数字是否与一个奇数和另一个偶数混合
- java - Java Boolean ...尽管布尔值发生变化,如何保持指向相同的引用
- python - 用于 75K 数据包/秒负载下的实时应用程序的 python 网络嗅探器