在Laravel的搜索表单中显示数据库结果的问题

时间:2019-07-08 13:26:31

标签: php laravel search-form

我正在尝试根据搜索表单中选定的过滤器显示数据库中的项目。当我放置一些过滤器并提交表单时,我什么都没得到,应该有一些与过滤器相对应的结果。我的网址相应更改,但没有结果。我有三个表,它们之间存在多对多关系。

properties (id, city, price, quadarature)

category_property (id, category_id, property_id)

categories (id, category, priority)

感谢您的帮助。这是我的代码。

startpage.blade.php

@extends('layouts.app')

@section('content')

<div class="container">

    <div class="row justify-content-md-center">

        <div class="col-md-8" id="forma">

            <form id="searchForm" method="GET" action="/">

                <div class="row">
                    <div class="col-md-12 mb-3">
                    <h5 class="text-center">City</h5>
                    <input name="city" list="result" id="input" class="form-control" value="{{ old('city') }}">
                    <datalist id="result"></datalist>
                    </div>
                </div>

                <div class="row">

                    <div class="col-md-3 mb-6">
                        <label>Price</label>
                        <input type="number" id="min_price" name="min_price" class="form-control" placeholder="Min Price" value="{{ old('min_price') }}">
                        <input type="number" id="max_price" name="max_price" class="form-control" placeholder="Max Price" value="{{ old('max_price') }}">
                    </div>

                    <div class="col-md-3 mb-6">
                        <label>Quadrature</label>
                        <input type="number" name="min_quadrature" class="form-control" placeholder="Min quadrature" value="{{ old('min_quadrature') }}">
                        <input type="number" name="max_quadrature" class="form-control" placeholder="Max quadrature" value="{{ old('max_quadrature') }}">
                    </div>

                    <div class="col-md-2 mb-6">
                        <h5>Payment Method</h4>
                        <div class="d-block my-3">
                            <div class="custom-control custom-radio">
                                <input id="ponuda" name="propertyBidAsk" value="ponuda" type="radio" class="custom-control-input" @if( old('propertyBidAsk')=='ponuda')checked @endif>
                                <label class="custom-control-label" for="ponuda">Ponuda</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="potraznja" name="propertyBidAsk" value="potraznja" type="radio" class="custom-control-input" @if( old('propertyBidAsk')=='potraznja')checked @endif>
                                <label class="custom-control-label" for="potraznja">Potraznja</label>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-2 mb-6">
                        <h5>Property payment</h4>
                        <div class="d-block my-3">
                            <div class="custom-control custom-radio">
                                <input id="kupovina" name="propertyPayment" value="kupovina" type="radio" class="custom-control-input"  @if( old('propertyPayment')=='Kupovina')checked @endif>
                                <label class="custom-control-label" for="kupovina">Kupovina</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="izdavanje" name="propertyPayment" value="izdavanje" type="radio" class="custom-control-input" @if( old('propertyPayment')=='Izdavanje')checked @endif>
                                <label class="custom-control-label" for="izdavanje">Izdavanje</label>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-2 mb-6">
                        <h5>Property type</h4>
                        <div class="d-block my-3 ">
                            <div class="custom-control custom-radio">
                                <input id="kuca" name="propertyType" value="kuca" type="radio" class="custom-control-input" @if( old('propertyPayment')=='kuca')checked @endif>
                                <label class="custom-control-label" for="kuca">Kuca</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="stan" name="propertyType" value="stan" type="radio" class="custom-control-input" @if( old('propertyPayment')=='stan')checked @endif>
                                <label class="custom-control-label" for="stan">Stan</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="apartman" name="propertyType" value="apartman" type="radio" class="custom-control-input" @if( old('propertyPayment')=='apartman')checked @endif>
                                <label class="custom-control-label" for="apartman">Apartman</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="soba" name="propertyType" value="soba" type="radio" class="custom-control-input" @if( old('propertyPayment')=='soba')checked @endif>
                                <label class="custom-control-label" for="soba">Soba</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="lokal" name="propertyType" value="lokal" type="radio" class="custom-control-input" @if( old('propertyPayment')=='lokal')checked @endif>
                                <label class="custom-control-label" for="lokal">Lokal</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="plac" name="propertyType" value="plac" type="radio" class="custom-control-input" @if( old('propertyPayment')=='plac')checked @endif>
                                <label class="custom-control-label" for="plac">Plac</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="garaza" name="propertyType" value="garaza" type="radio" class="custom-control-input" @if( old('propertyPayment')=='garaza')checked @endif>
                                <label class="custom-control-label" for="garaza">Garaza</label>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="row justify-content-md-center">
                    <div class="col-md-2 mb-6">
                        <button class="btn btn-primary btn-lg btn-block" type="submit">Search</button>
                    </div>
                </div>

                 <hr class="mb-4">

            </form>
        </div>

    </div>

</div>

    <div class="row justify-content-md-center">
        <div>
            @if(isset($results))
                <table class="table">
                <thead>
                    <th>Property Bid Ask</th>
                    <th>Property Payment</th>
                    <th>Property Type</th>
                    <th>City</th>
                    <th>Price</th>
                    <th>Quadrature</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>
                        <td>{{ $result->city }}</td>
                        <td>{{ $result->price }}</td>
                        <td>{{ $result->quadrature }}</td>
                    </tr>
                    @endforeach
                </tbody>
                </table>
            @endif
        </div>
    </div>

</div>

@endsection

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()
    {
        $properties = Property::where('active', '1')
            ->orderBy('page_views', 'desc')
            ->with('user.photo', 'photos', 'category')
            ->paginate(10);

        return view('startpage', compact('properties'));
    }

    public function search(Request $request, $propertyBidAsk, $propertyPayment, $propertyType, $city, $price, $quadrature, Property $property)
    {     

        $category = $property->category;

        $query = Property::query();

        if ($request->filled('propertyBidAsk')) {
            $propertyBidAsk = $request->get('propertyBidAsk');
        }

        if ($propertyBidAsk) {
            $query->whereHas('category', function ($query) use ($request) {
                $query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
            });
        }

        if ($request->filled('propertyPayment')) {
            $propertyPayment = $request->get('propertyPayment');
        }

        if ($propertyPayment) {
            $query->whereHas('category', function ($query) use ($request) {
                $query->where('category', 'like', '%' . $request->propertyPayment . '%');
            });
        }

        if ($request->filled('propertyType')) {
            $propertyType = $request->get('propertyType');
        }

        if ($propertyType) {
            $query->whereHas('category', function ($query) use ($request) {
                $query->where('category', 'like', '%' . $request->propertyType . '%');
            });
        }

        if ($request->filled('city')) {
            $city = $request->get('city');
        }

        if ($city) {
            $query->where('city', 'LIKE', '%' . $request->city . '%');
        }

        // PRICE

    $array = explode('-', $price);

        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 ($request->filled('min_price')) {
            $min_price = $request->get('min_price');
        }

        if ($request->filled('max_price')) {
            $max_price = $request->get('max_price');
        }

        if ($min_price && $max_price) {
            $query->whereBetween('price', [$min_price, $max_price]);
        }
        elseif ($min_price) {
            $query->where('price', '>=', $min_price);

        }
        elseif ($max_price) {
            $query->where('price', '<=', $max_price);

        }

        // QUADRATURE

        $array1 = explode('-', $quadrature);

        foreach ($array1 as $a1) {
            $s1 = [];
            $s1 = explode('=', $a1);
            if ($s1[0] === 'min_quadrature') {
                $s1[1] ? $min_quadrature = intval($s1[1]) : $min_quadrature = null;
            } else {
               $s1[1] ? $max_quadrature = intval($s1[1]) : $max_quadrature = null;
            }
        }

        if ($request->filled('min_quadrature')) {
            $min_quadrature = $request->get('min_quadrature');
        }

        if ($request->filled('max_quadrature')) {
            $max_quadrature = $request->get('max_quadrature');
        }

        if ($min_quadrature && $max_quadrature) {
            $query->whereBetween('quadrature', [$min_quadrature, $max_quadrature]);
        }
        elseif ($min_quadrature) {
            $query->where('price', '>=', $min_quadrature);

        }
        elseif ($max_quadrature) {
            $query->where('quadrature', '<=', $max_quadrature);

        }

        $results = $query->get();

        return view('startpage', compact('category', 'results'));
    }
}

web.php

Route::get('/', 'CategoryController@index')->name('startpage');

Route::get('/{propertyBidAsk}/{propertyPayment}/{propertyType}/
{city}/{price}/{quadrature}', 'CategoryController@search')->name('search.param');

0 个答案:

没有答案