传入对象的值然后搜索该对象

时间:2018-05-02 15:19:32

标签: laravel laravel-5 eloquent request query-builder

我有一个搜索工作,找到其首选项与房东属性匹配的用户。这一切都很好,但我希望房东能够选择一个属性,然后按搜索,并传递该属性对象进行比较。

这是搜索图片

Screenshot

用于在搜索中搜索从列表中选择的任何属性。

这是搜索视图

<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="{{$property->address . ', ' . $property->town . ', ' . $property->county}}">{{$property->address . ', ' . $property->town . ', ' . $property->county}}</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>

这是控制器

第一部分呈现搜索表单

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'));}

接下来是逻辑

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'));
  }

我尝试使用请求对象将所选属性设置为与之比较的属性。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不是使用地址填充选择值,而是使用属性的ID填充它,然后通过从select传递的ID从数据库中获取该属性。

$property = Property::find($request->property_id);


$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();