从查询中获取空集合

时间:2016-04-06 15:08:44

标签: laravel laravel-5 laravel-5.1

我有这个问题:

$amount = Input::get('amount'); // First getting the age range from the input in html (name="amount").
        $amount = str_replace(' ','',$amount); //remove the blank spaces between the ages value, if there's any.
        $amount = explode('-',$amount); // is breaking the value 16-45, as 16 and 45, '-' being the breaking point. And finally turns the value into array.

        // Getting the array value as index 0 and 1.
        $min = $amount[0];
        $max = $amount[1];

        $user = $request->user();
        $userSelect = Input::get('select_preferences');
        $prefQuery = Profile::whereHas('expectations', function($query) use($userSelect) {
        $query->whereIn('name', $userSelect); // First Query: compare the name column in expectations table with the user selection.
        //now chain another query:
        })->whereBetween('age', [$min,$max])    
        ->with('user', 'expectations')->get();

我正在努力获得用户选择和年龄范围。运行dump($prefQuery);时 - 我收到空集:

Collection {#192 ▼
  #items: []
}

以下是要查询的个人资料模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class profile extends Model
{
  protected $table = 'profiles';

  protected $fillable = ['gender','age','goals','activityType'];


  /**
   * A profile belongs to many Expectations
   * @return Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function expectations()
  {
      return $this->belongsToMany('App\Expectation');
  }

  /**
   * A profile is belong to a user
   * @return Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function user()
  {
    return $this->belongsTo('App\User'); //belongsTo relationship from your Profile model to your user model.
  }
}

为什么我得到一个空的集合,尽管我可以进行最广泛的搜索?

0 个答案:

没有答案