zoukankan      html  css  js  c++  java
  • 关于 laravel 集合的使用

    常用的有

    count()

    count方法返回集合中所有项的数目:

    $collection = collect([1, 2, 3, 4]);
    $collection->count();
    // 4

    forPage()

    forPage方法返回新的包含给定页数数据项的集合:

    $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9])->forPage(2, 3);
    
    $collection->all();
    // [4, 5, 6]

    map()

    map方法遍历集合并传递每个值给给定回调。该回调可以修改数据项并返回,从而生成一个新的经过修改的集合:

    1 $collection = collect([1, 2, 3, 4, 5]);
    2 
    3 $multiplied = $collection->map(function ($item, $key) {
    4     return $item * 2;
    5 });
    6 
    7 $multiplied->all();
    8 // [2, 4, 6, 8, 10]

    groupBy()

    groupBy方法通过给定键分组集合数据项:

    $collection = collect([
        ['account_id' => 'account-x10', 'product' => 'Chair'],
        ['account_id' => 'account-x10', 'product' => 'Bookcase'],
        ['account_id' => 'account-x11', 'product' => 'Desk'],
    ]);
    
    $grouped = $collection->groupBy('account_id');
    
    $grouped->toArray();
    
    /*
    [
        'account-x10' => [
            ['account_id' => 'account-x10', 'product' => 'Chair'],
            ['account_id' => 'account-x10', 'product' => 'Bookcase'],
        ],
        'account-x11' => [
            ['account_id' => 'account-x11', 'product' => 'Desk'],
        ],
    ]
    */

    filter()

    filter方法通过给定回调过滤集合,只有通过给定测试的数据项才会保留下来:

    $collection = collect([1, 2, 3, 4]);
    
    $filtered = $collection->filter(function ($item) {
        return $item > 2;
    });
    
    $filtered->all();
    // [3, 4]

    collapse()

    collapse方法将一个多维数组集合收缩成一个一维数组:

    $collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    $collapsed = $collection->collapse();
    $collapsed->all();
    // [1, 2, 3, 4, 5, 6, 7, 8, 9]

    flatten()

    flatten方法将多维度的集合变成一维的:

    $collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]);
    $flattened = $collection->flatten();
    $flattened->all();
    // ['taylor', 'php', 'javascript'];

    注意:collapse() 与 flatten() 效果是不相同的;

    last()

    last方法返回通过测试的集合的最后一个元素:

    collect([1, 2, 3, 4])->last(function ($key, $value) {
        return $value < 3;
    });
    // 2

    merge()

    merge方法合并给定数组到集合。该数组中的任何字符串键匹配集合中的字符串键的将会重写集合中的值:

    $collection = collect(['product_id' => 1, 'name' => 'Desk']);
    $merged = $collection->merge(['price' => 100, 'discount' => false]);
    $merged->all();
    // ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]

    pluck()

    pluck方法为给定键获取所有集合值:

    $collection = collect([
        ['product_id' => 'prod-100', 'name' => 'Desk'],
        ['product_id' => 'prod-200', 'name' => 'Chair'],
    ]);
    
    $plucked = $collection->pluck('name');
    
    $plucked->all();
    // ['Desk', 'Chair']

    search()

    search方法为给定值查询集合,如果找到的话返回对应的键,如果没找到,则返回false

    $collection = collect([2, 4, 6, 8]);
    $collection->search(4);
    // 1

    上面的搜索使用的是松散比较,要使用严格比较,传递true作为第二个参数到该方法:

    $collection->search('4', true);
    // false

    sum()

    sum方法返回集合中所有数据项的和:

    collect([1, 2, 3, 4, 5])->sum();
    // 15

    toArray()

    toArray方法将集合转化为一个原生的PHP数组。如果集合的值是Eloquent模型,该模型也会被转化为数组:

    $collection = collect(['name' => 'Desk', 'price' => 200]);
    $collection->toArray();
    
    /*
        [
            ['name' => 'Desk', 'price' => 200],
        ]
    */

    注意:toArray还将所有嵌套对象转化为数组。如果你想要获取底层数组,使用all方法。

    toJson()

    toJson方法将集合转化为JSON:

    $collection = collect(['name' => 'Desk', 'price' => 200]);
    
    $collection->toJson();
    // '{"name":"Desk","price":200}'

    unique()

    unique方法返回集合中所有的唯一数据项:

    $collection = collect([1, 1, 2, 2, 3, 4, 2]);
    $unique = $collection->unique();
    $unique->values()->all();
    // [1, 2, 3, 4]

    可查询资料  http://laravelacademy.org/post/178.html#toc_10

    【Tag: collection】http://laravelacademy.org/post/178.html#ipt_kb_toc_178_3      

    【 Laravel 5.1 文档 ] 服务 —— 集合】http://laravelacademy.org/post/178.html#ipt_kb_toc_178_49

     【 Laravel 5.4 文档 ] 综合话题 —— 集合】http://laravelacademy.org/post/6863.html

    世事洞明皆学问
  • 相关阅读:
    sqlalchemy 查询姿势总结
    sqlalchemy 常用总结
    rsyslog 移植与配置方案介绍
    软件设计随想录
    C语言面对对象设计模式汇编
    关于linux kernel slab内存管理的一点思考
    linux PMBus总线及设备驱动分析
    Linux x86_64 APIC中断路由机制分析
    单板控制领域模型设计与实现
    Linux mips64r2 PCI中断路由机制分析
  • 原文地址:https://www.cnblogs.com/Json159/p/9570903.html
Copyright © 2011-2022 走看看