mercredi 2 mai 2018

Passing in value of object then searching against that object

I have a search working that finds users whose preferences match a landlords property. This is all well and good, but I want the landlord to be able to select a property, then press search, and pass that property object to be compared against.

This is an image of the search

Whichever property is chosen from the list is used to be searched against in the search.

This is the view of the search

<div class="row">
                <div class="col-md-10">
                  <select class="form-control" id="property_address" name="property_address">
                    <!--Gets all counties from DB -->
                    @foreach ($properties as $property)
                      <option value=""></option>
                    @endforeach
                  </select>
                </div> <!-- ./ col-6-->
              </div> <!-- ./ row-5  -->

            <div class="row mt-md-4">
                <div class="col-md-4">
                    <button type="submit" class="form-control btn btn-primary">
                        Search
                    </button>
                </div>
            </div>

This is the controller

The first part renders the search form

public function searchhome(){
      //Search Filter UI
      //Populates fields.
      $user = Auth::user();
      $properties = PropertyAdvert::where('user_id', Auth::id())->get();
      return view('pages/account/search/index', compact('user', 'properties'));}

Next is the logic

public function searchresults(Request $request){

    //Gets all users that are tenants
    $tenants = User::where('userType', 'tenant')->first();

    //Gets all preferances
    $Prefereances = TenantPreferance::all()->first();
    //Gets the prefereances that match a tenant id
    $pref = $Prefereances::where('user_id', $tenants->id)->first();

    //Gets the current signed in users property

    //Attempting to get value of property by name
    $property = $request->property_address;

    $result = $pref
              ->where('county' , $property->county)
              ->where('type' , $property->type)
              ->where('rent', '<=', $property->rent)
              ->where('bedrooms', '<=', $property->bedrooms)
              ->where('bathrooms', '<=', $property->bathrooms);

    $users = $result->get();

    return view('pages/account/search/results', compact('users'));
  }

I tried using the request object to set the selected property as the one being compared against.

Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire