【问题标题】:Passing in value of object then searching against that object传入对象的值然后搜索该对象
【发布时间】:2018-05-02 15:19:32
【问题描述】:

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

这是搜索的图片

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

这是搜索视图

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

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

有什么想法吗?

【问题讨论】:

  • 您的帖子中是否缺少某些代码?如果$property 是表单的输入,则它不会是具有该特定属性值的对象。

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


【解决方案1】:

不是用地址填充选择值,而是用属性的 ID 填充它,然后通过从选择传递的 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多