mercredi 6 avril 2016

Getting Empty Collection From A Query

I have this query:

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

I am trying to get user selection and age range. When running dump($prefQuery); - I am getting empty collection:

Collection {#192 ▼
  #items: []
}

Here's the profile model being queried:

<?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.
  }
}

Why am I getting an empty collection although I am making the widest search possible?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire