1

I have search form to list properties/ads through certain criteria (property Bid-Ask, Property Payment, Property Type, price, quadrature`). When I select all the criteria that there are, it shows all and correct results in the table. The problem is when I select one or two criteria, it should return all properties that have just those two criteria, but instead I get redirected to 404 not found page. Also, my URL, then works fine, it shows the right URL, just with 404. I have three tables.

properties (id, city, price, quadrature, property_type)
categories (id, category, priority)
category_property (id, property_id, category_id)

Any help is greatly appreciated. Here is my code:

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');
17
  • I think one of the reason is when the others is empty so your url will change and your route will not recognize it. Commented Jun 28, 2019 at 8:09
  • @JovenSalvador How should I change my code in order that to work? Commented Jun 28, 2019 at 8:10
  • did that solved the problem or not? Commented Jun 28, 2019 at 8:33
  • @JovenSalvador It helped, I get the idea, I just can't seem to put it right in code Commented Jun 28, 2019 at 8:39
  • just ask. maybe I can help more :) Commented Jun 28, 2019 at 8:45

1 Answer 1

1

You can do it like this. instead of using / you can use ?

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;

then your web.php

Route::get('/search', 'CategoryController@index');

Route::get('/search/addsomethinghere', 'CategoryController@search');

then in your controller you can use them as a request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.