【问题标题】:Problem with filter in my search form in Laravel我在 Laravel 中的搜索表单中的过滤器问题
【发布时间】:2019-07-03 06:56:26
【问题描述】:

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

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

【问题讨论】:

    标签: php laravel


    【解决方案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();
    

    【讨论】:

    • 当我选择多个过滤器并单击提交时,我得到空表(没有结果),应该有一些东西。
    • @Gacho 那么这意味着没有基于这些过滤器组合的结果,并且可能还有其他问题。类别如何保存到属性中?他们在不同的桌子上吗?或者它们只是属性的一个字段?如果是第二个,可以去掉$query-&gt;whereHas
    • 肯定有结果。表格是属性(id、城市、价格)、category_property(id、category_id、property_id)、类别(id、category、priority)。关系是多对多的,那里的一切都有效。此外,当我通过单个过滤器进行搜索时,它可以工作,当我组合过滤器时,我只会得到空结果
    • @Gacho 我更新了我的答案。我想我之前的答案是尝试将单个类别与所有 3 个过滤器匹配,而不是检查不同的类别。答案的想法保持不变,在顶部开始查询,然后有条件地添加过滤器,然后得到结果。
    • 就是这样。有效!
    【解决方案2】:

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

    【讨论】:

    • 你能帮我写代码吗?我理解这个想法,只是无法让代码工作
    • $property = [];在顶部和每个 if 块中将 $property 替换为 $property[] @Gacho
    • 此集合实例上不存在属性 [类别]。提交表单时出现此错误。
    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多