简体   繁体   English

在Laravel集合中使用where函数

[英]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: 我有一个带有设计的数组配置,每个设计都对应一个主要标签类别,但是有些设计有两个主要标签,例如pokemon +王座游戏:

'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. 我正在使用where函数。 For example, category_design_name in this case is 'game-of-thrones': 例如,在这种情况下,category_design_name是“权力游戏”:

$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: 这会返回仅具有main_tag =权力游戏的设计,但不会返回具有多个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 这返回我: 未定义的变量:category_design_name

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM