Laravel搜索表单中的过滤器出现问题

时间:2019-07-03 06:56:26

标签: php laravel

我有搜索表单,可以通过某些条件列出属性/广告。我的搜索表单有效,但是当我使用多个过滤器进行搜索时却没有得到想要的结果。我有三种类型的过滤器。物业要价(要价,要约),物业付款(买,租),物业类型(房屋,公寓,车库)。例如,当我单击按需求,购买,住房进行搜索时,它返回一个结果,其中存在需求,购买,住房,而在存在购买和住房的情况下返回另外两个结果,但是第三个参数是offer。我很努力地希望过滤器满足所有三个条件,而不仅仅是两个。任何帮助表示赞赏。这是我的代码。

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

2 个答案:

答案 0 :(得分:1)

我认为在这种情况下,如果多个条件都成立,那么您将覆盖变量$property,那么即使您的第一个查询运行,它将替换第二个if condition中的所有数据 要解决此问题,请尝试将所有三个查询result of ->get()存储在数组中并返回该数据数组。 您必须首先在顶部分配一个空数组,并在条件成立时将相应的数据相应地存储在数组中。

答案 1 :(得分:1)

如果要有条件地在一个查询上同时添加多个过滤器,则可以从顶部开始查询,然后添加过滤器,最后在最后得到结果:

$query = Property::query();
if (!empty($request->propertyBidAsk)) {
    $query->whereHas('category', function ($query) use ($request) {
        $query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
    });
}
if (!empty($request->propertyPayment)) {
    $query->whereHas('category', function ($query) use ($request) {
        $query->where('category', 'like', '%' . $request->propertyPayment . '%');
    });
}
if (!empty($request->propertyType)) {
    $query->whereHas('category', function ($query) use ($request) {
        $query->where('category', 'like', '%' . $request->propertyType . '%');
    });
}
$property = $query->get();