我有一个搜索表单,用于通过某些条件(物业出价,物业付款,物业类型,价格,正交)列出物业/广告。当我选择所有存在的条件时,它会在表中显示所有正确的结果。问题是当我选择一个或两个条件时,它应该返回仅具有这两个条件的所有属性,但是我将重定向到404 not found页面。另外,我的URL可以正常工作,它只显示404即可显示正确的URL。我有三个表。
properties (id, city, price, quadrature, property_type)
categories (id, category, priority)
category_property (id, property_id, category_id)
任何帮助将不胜感激。这是我的代码:
search.blade.php
<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>
<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>
<form id="searchForm" method="GET" action="/search">
<div class="row">
<div class="col-md-5 mb-3">
<label>City</label>
<input name="city" list="result" id="input" class="form-control">
<datalist id="result"></datalist>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-6">
<label>Price</label>
<input type="number" name="min_price" class="form-control" placeholder="Min Price">
<input type="number" name="max_price" class="form-control" placeholder="Max Price">
</div>
<div class="col-md-6 mb-6">
<label>Quadrature</label>
<input type="number" name="min_quadrature" class="form-control" placeholder="Min quadrature">
<input type="number" name="max_quadrature" class="form-control" placeholder="Max quadrature">
</div>
</div>
<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="ponuda" name="propertyBidAsk" value="ponuda" type="radio" class="custom-control-input">
<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">
<label class="custom-control-label" for="potraznja">Potraznja</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="kupovina" name="propertyPayment" value="kupovina" type="radio" class="custom-control-input">
<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">
<label class="custom-control-label" for="izdavanje">Izdavanje</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">House</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>
</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;
var city = this['city'].value.trim() || 0;
var min_price = this["min_price"].value.trim() || 0;
var max_price = this["max_price"].value.trim() || 0;
var price = min_price + "_" + max_price;
var min_quadrature = this["min_quadrature"].value.trim() || 0;
var max_quadrature = this["max_quadrature"].value.trim() || 0;
var quadrature = min_quadrature + "_" + max_quadrature;
url = propertyBidAsk.length === 0 ? '' : ( '/' + encodeURIComponent(propertyBidAsk) );
url += propertyPayment.length === 0 ? '' : ( '/' + encodeURIComponent(propertyPayment) );
url += propertyType.length === 0 ? '' : ( '/' + encodeURIComponent(propertyType) );
url += city.length === 0 ? '' : ( '/' + encodeURIComponent(city) );
url += min_price.length === 0 ? '' : ( '/min_price' + "=" + encodeURIComponent(min_price) );
url += max_price.length === 0 ? '' : ( '-max_price' + "=" + encodeURIComponent(max_price) );
url += min_quadrature.length === 0 ? '' : ( '/min_quadrature' + "=" + encodeURIComponent(min_quadrature) );
url += max_quadrature.length === 0 ? '' : ( '-max_quadrature' + "=" + encodeURIComponent(max_quadrature) );
window.location.href = this.action + url;
}
document.addEventListener( 'DOMContentLoaded', function(){
var srch = document.getElementById("searchForm");
srch.addEventListener('submit', onSubmitFunc, false);
}, false );
</script>
</div>
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Http\Controllers\Controller;
use App\Property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
class CategoryController extends Controller
{
public function index()
{
$data = \DB::table('properties');
return view('categories.search', compact('data'));
}
public function search($propertyBidAsk, $propertyType, $propertyPayment, $city, $price, $quadrature, Request $request, Property $property)
{
$category = $property->category;
$property_obj = Property::query();
if (!empty($request->city)) {
$property_obj->where('city', 'LIKE', "%" . $request->city . "%");
}
if (!empty($request->min_price) && !empty($request->max_price)) {
$property_obj->whereBetween('price', [$min_price, $max_price]);
}
if (!empty($request->min_quadrature) && !empty($request->max_quadrature)) {
$property_obj->whereBetween('quadrature', [$min_quadrature, $max_quadrature]);
}
$results = $property_obj->get();
dd($request->all());
return view('categories.search', compact('category', 'results'));
}
}
web.php
Route::get('/search', 'CategoryController@index');
Route::get('/search/{propertyBidAsk}/{propertyPayment}/{propertyType}/
{city}/{price}/{quadrature}', 'CategoryController@search');
答案 0 :(得分:1)
您可以这样做。代替使用/,您可以使用?
url = propertyBidAsk.length === 0 ? '' : ( '?search1=' + encodeURIComponent(propertyBidAsk) );
url += propertyPayment.length === 0 ? '' : ( '?search2=' + encodeURIComponent(propertyPayment) );
url += propertyType.length === 0 ? '' : ( '?search3=' + encodeURIComponent(propertyType) );
url += city.length === 0 ? '' : ( '?search4=' + encodeURIComponent(city) );
url += min_price.length === 0 ? '' : ( '?search5=' + "=" + encodeURIComponent(min_price) );
url += max_price.length === 0 ? '' : ( '?search6=' + "=" + encodeURIComponent(max_price) );
url += min_quadrature.length === 0 ? '' : ( '?search7=' + "=" + encodeURIComponent(min_quadrature) );
url += max_quadrature.length === 0 ? '' : ( '?search8=' + "=" + encodeURIComponent(max_quadrature) );
window.location.href = search/addsomethinghere + url;
然后您的web.php
Route::get('/search', 'CategoryController@index');
Route::get('/search/addsomethinghere', 'CategoryController@search');
然后在您的控制器中,您可以将它们用作请求。