Laravel Collection,用filter方法替换where方法

I have a collection on which I use the where method in order to get only the element that match like this:

myCollection->where('akey', $value);

When I try to transform this with the filter method, it fails:

myCollection->filter(function($value, $key){
   if($key === 'akey' && $value === $aValue)
      return true;
});

I try to use filter because I want to select items in the collection if theirs values are equals to multiple value. (where or Where or Where or Where basically).

I assume that $aValue is defined outside of the filter function, you should pass it to the callback function like this:

myCollection->filter(function($value, $key) use ($aValue) {
  if($key === 'akey' && $value === $aValue)
      return true;
});

-- EDIT --

Based on your example using where I think this should work.

myCollection->filter(function($item) use ($aValue) {
   return $item->aKey === $aValue;
});

I write a simple PHP script to describe your problem, Please check out the inline comments for details.

<?php

require __DIR__ . '/vendor/autoload.php';

use Illuminate\Support\Collection;

$data = [
    [
        'akey' => 'the answer',
        'avalue' => 'vote up',
    ],
];

$filterKey = 'the answer';

$c = new Collection($data);
$c = $c->filter(function($val, $key) use ($filterKey) {
    var_dump($key); // $key is indexed number, It will output "int(0)"
    // return $key == $filterKey; // XXX Here is problem, Try to comment out to see the different.
    return $val['akey'] == $filterKey; // Get value from $val, which is associative array. 
});

print_r($c->all());

Output:

/Users/gasolwu/Code/laravel/demo.php:18:
int(0)
Array
(
    [0] => Array
        (
            [akey] => the answer
            [avalue] => vote up
        )

)