有没有办法可以简化这个?

I'm using a Laravel Eloquent Collection filter. I'm pretty sure this can be simplified but I'm not sure exactly. I am using all the variables after this chunk of code but I'd like to refactor so it's nicer.

    $cards = new Collection($data);

    $whiteBlueBlackRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black","Red","Green"]') { return true; }});

    $whiteBlueBlackRed = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black","Red"]') { return true; }});
    $whiteBlueBlackGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black","Green"]') { return true; }});
    $whiteBlueRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Red","Green"]') { return true; }});
    $whiteBlackRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Black","Red","Green"]') { return true; }});
    $blueBlackRedGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Black","Red","Green"]') { return true; }});

    $whiteBlueBlack = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black"]') { return true; }});
    $whiteBlueRed = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Red"]') { return true; }});
    $whiteBlueGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Green"]') { return true; }});
    $whiteBlackRed = $cards->filter(function($card) { if ($card->colors == '["White","Black","Red"]') { return true; }});
    $whiteBlackGreen = $cards->filter(function($card) { if ($card->colors == '["White","Black","Green"]') { return true; }});
    $whiteRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Red","Green"]') { return true; }});
    $blueBlackRed = $cards->filter(function($card) { if ($card->colors == '["Blue","Black","Red"]') { return true; }});
    $blueBlackGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Black","Green"]') { return true; }});
    $blueRedGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Red","Green"]') { return true; }});
    $blackRedGreen = $cards->filter(function($card) { if ($card->colors == '["Black","Red","Green"]') { return true; }});

    $whiteBlue = $cards->filter(function($card) { if ($card->colors == '["White","Blue"]') { return true; }});
    $whiteBlack = $cards->filter(function($card) { if ($card->colors == '["White","Black"]') { return true; }});
    $whiteRed = $cards->filter(function($card) { if ($card->colors == '["White","Red"]') { return true; }});
    $whiteGreen = $cards->filter(function($card) { if ($card->colors == '["White","Green"]') { return true; }});
    $blueBlack = $cards->filter(function($card) { if ($card->colors == '["Blue","Black"]') { return true; }});
    $blueRed = $cards->filter(function($card) { if ($card->colors == '["Blue","Red"]') { return true; }});
    $blueGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Green"]') { return true; }});
    $blackRed = $cards->filter(function($card) { if ($card->colors == '["Black","Red"]') { return true; }});
    $blackGreen = $cards->filter(function($card) { if ($card->colors == '["Black","Green"]') { return true; }});
    $redGreen = $cards->filter(function($card) { if ($card->colors == '["Red","Green"]') { return true; }});

    $white = $cards->filter(function($card) { if ($card->colors == '["White"]') { return true; }});
    $blue = $cards->filter(function($card) { if ($card->colors == '["Blue"]') { return true; }});
    $black = $cards->filter(function($card) { if ($card->colors == '["Black"]') { return true; }});
    $red = $cards->filter(function($card) { if ($card->colors == '["Red"]') { return true; }});
    $green = $cards->filter(function($card) { if ($card->colors == '["Green"]') { return true; }});

Well, you could factor out the filtering a bit:

function getCards($cards, $colors) {
    return $cards->filter(function($card) use ($colors) {
        if ($card->colors == $colors) { return true; }
    });
}

$whiteBlueBlackRedGreen = getCards($cards, '["White","Blue","Black","Red","Green"]');
// and so on...

That would avoid a lot of repetitive typing and make the code easier to read.


Going deeper down the rabbit hole...

$cards = new Collection($data);

function getCards($cards, $colors) {
    return $cards->filter(function($card) use ($colors) {
        if ($card->colors == $colors) { return true; }
    });
}

$colors = array("White", "Blue", "Black", "Red", "Green");
$coloredCards = array();
foreach($colors as $color) {
    foreach($coloredCards as $existingCombo => $existingCards) {
        $newKey = $existingCombo . "-" . $color;
        $coloredCards[$newKey] = getCards($cards, json_encode(explode("-", $newKey)));
    }
    $coloredCards[$color] = getCards($cards, '["' . $color . '"]');
}

I haven't actually tested this, but it should get you an array with keys like $coloredCards['White-Blue-Black'] that contains what your variables would have.

Edit: I've tested a version that doesn't actually call getCards to make sure it's the proper set of combinations - you can see the test here: http://ideone.com/Uk6H5x