Search code examples
phplaravellaravel-5laravel-5.3

Use where function in a Laravel Collection


I have a array config with designs and each design correspond to a main tag category but some designs have two main tags for example pokemon + game of thrones:

'b5u8ic66ywua' => array(
        'design_id'     => 'b5u8ic66ywua',
        'design_name' => 'choose dragon blanket',
        'main_tag'    => 'pokemon,game of thrones',
        'tags'        => array('pokemon','pixelart','game of thrones'),
        'article_owner' => '1426',

    ),
    'dx6nalh1bkk4' => array(
        'design_id'     => 'dx6nalh1bkk4',
        'design_name' => 'choose direwolf blanket',
        'main_tag'    => 'pokemon,game of thrones',
        'tags'        => array('pokemon','pixelart','game of thrones'),
        'article_owner' => '1426',

    ), 
    'ih91h1i77g9j' => array(
        'design_id'     => 'ih91h1i77g9j',
        'design_name'   => 'game of thrones pixelart',
        'main_tag'      => 'game of thrones',
        'tags'          => array('game of thrones','stark','jon snow','daenerys', 'danny','pixelart','game of thrones'),
        'article_owner' => '1426',

    ),

    'wolcf2m524nu' => array(
        'design_id'     => 'wolcf2m524nu',
        'design_name'   => 'starks revenge',
        'main_tag'      => 'game of thrones',
        'tags'          => array('game of thrones','stark','house stark','starks'),
        'article_owner' => '1426',

    ),

In this case, I want get all designs that contains the main tag as game of thrones, and create a collection with these designs.

I´m using where function. For example, category_design_name in this case is 'game-of-thrones':

$family_article_designs = collect(config('artdesigns.'.$article_id))->where('main_tag',str_replace("-", " ", $category_design_name));

$family_article_designs = $family_article_designs->all();

This return me the designs with only main_tag=game of thrones, but not return me the designs with more than one main_tag:

'ih91h1i77g9j' => array(
        'design_id'     => 'ih91h1i77g9j',
        'design_name'   => 'game of thrones pixelart',
        'main_tag'      => 'game of thrones',
        'tags'          => array('game of thrones','stark','jon snow','daenerys', 'danny','pixelart','game of thrones'),
        'article_owner' => '1426',

    ),

    'wolcf2m524nu' => array(
        'design_id'     => 'wolcf2m524nu',
        'design_name'   => 'starks revenge',
        'main_tag'      => 'game of thrones',
        'tags'          => array('game of thrones','stark','house stark','starks'),
        'article_owner' => '1426',

    ),

I want get all the designs, that contains in his the word 'game of thrones'.

EDIT:

$family_article_designs = collect(config('artdesigns.'.'@'.$article_id))->filter(function ($value, $key) {
                            if(strpos($value['main_tag'], str_replace("-", " ", $category_design_name)) !== false)
                             return true;
                        });

This return me: Undefined variable: category_design_name


Solution

  • You forgot to use variable name:

    $family_article_designs = collect(config('artdesigns.'.'@'.$article_id))->filter(function ($value, $key) use($category_design_name) {
        if(strpos($value['main_tag'], str_replace("-", " ", $category_design_name)) !== false)
         return true;
    });