laravel 集合常用方法

    技术2022-07-11  130

    laravel 集合常用方法

    all()

    //创建集合 $collection = collect(['abc','fengge','wuge']); //以底层数组形式输出 return $collection;//["abc","fengge","wuge"] return $collection->all();["abc","fengge","wuge"]

    map

    //map 方法,类似访问器,可修改输出 return $collection->reject(function ($value,$key){ return $value === 'abc'; }) //["abc-0","fengge-1","wuge-2"]

    reject

    //支持链式,reject移出true 的值 return $collection->reject(function ($value,$key){ return $value === 'abc'; }) //{"1":"fengge","2":"wuge"} //支持链式,reject移出非true 的值 return $collection->reject(function ($value,$key){ return $value != 'abc'; }) //["abc"]

    filter

    //filter筛选为true 的值,和reject 相反 return $collection->filter(function ($value,$key){ return $value === 'abc'; }) //["abc"]

    search

    //search找到后返回key,找不到返回false return $collection->search('abc');

    each

    //迭代输 $collection->each(function ($item, $key) { echo $item; echo '--';}); //abc--fengge--wuge--

    chunk

    //集合的分割 return $collection->chunk(2); [["abc","fengge"],{"2":"wuge"}]

    自定义方法

    //use Illuminate\Support\Collection; //果三十多个方法都没有你要的,还可以自定义方法,比如说所有英文大写; $collection = collect(['Mr.Zhang', '李四', '王五', null]); Collection::macro('toUpper', function () { //dd($this); return $this->map(function ($value) { return strtoupper($value); }); }); return $collection->toUpper(); //["MR.ZHANG","\u674e\u56db","\u738b\u4e94",""]

    avg

    //返回平均值 $collection = collect([1, 2, 3, 4]); return $collection->avg(); //2.5 //返回分组平均值 $collection = collect([['男'=>1], ['女'=>1], ['男'=>3]]); return $collection->avg('男'); //2

    count

    //返回集合总数; $collection = collect([1, 2, 3, 4]); return $collection->count() //4

    PS:相关的还有 sum()、min()、max()等统计

    countBy()

    //返回数值出现的次数或回调函数指定值出现的次数 //值出现的次数 $collection = collect([1, 2, 2, 3, 4, 4, 4]); return $collection->countBy(); //{"1":1,"2":2,"3":1,"4":3} //回调搜索相同指定片段的值的次数 $collection = collect(['xiaoxin@163.com', 'yihu@163.com', 'xiaoying@qq.com']); return $collection->countBy(function ($value) { return substr(strrchr($value, '@'), 1); }); //{"163.com":2,"qq.com":1}

    diff()

    //返回集合数组之间不相同的部分,组合新的集合; //diff返回两个集合中不相同的 $collection = collect([1, 2, 3, 4, 5]); return $collection->diff([3, 5]); //{"0":1,"1":2,"3":4}

    duplicates()

    //返回重复的值; $collection = collect([1, 2, 2, 3, 4, 5, 5, 6]); return $collection->duplicates(); //严格派生方法:duplicatesStrict() {"2":2,"6":5}

    flatten()

    //将多维数组转换为一维 $collection = collect(['name'=>'Mr.Lee', 'details'=>['gender'=>'男', 'age'=>100]]); return $collection->flatten(); //["Mr.Lee","\u7537",100]

    get()

    //get()通过键名找值 $collection = collect(['name'=>'Mr.Lee', 'gender'=>'男']); return $collection->get('name'); //Mr.Lee

    has()

    //判断集合中是否存在指定键; $collection = collect(['name'=>'Mr.Lee', 'gender'=>'男']); return $collection->has('name'); //1

    pop()

    移出集合中最后一个值 $collection = collect([1, 2, 3, 4, 5]); $collection->pop(); return $collection; //[1,2,3,4]

    PS:相关的还有 pull()、push()、put()方法

    slice()

    //返回指定值后续的集合; $collection = collect([1, 2, 3, 4, 5]); return $collection->slice(3);//{"3":4,"4":5}

    sort()

    //返回指定值后续的集合; $collection = collect([3, 1 , 5, 2, 7]); return $collection->sort()->values(); //需要配合 values()方法 //[1,2,3,5,7]

    PS:类似的有 sortBy()、sortByDesc()、sortKeys()等

    where()

    //系列方法,和数据库条件一样; $collection = collect([ ['name'=>'Mr.Lee', 'gender'=>'男'], ['name'=>'Miss.Zhang', 'gender'=>'女'] ]); return $collection->where('name', 'Mr.Lee'); //[{"name":"Mr.Lee","gender":"\u7537"}]

    模型集合

    map()

    //通过它可以实现类似访问器一样对字段进行处理的效果 $users = User::get(); $women = $users->map(function ($user) { $user->email = strtoupper($user->email); return $user; }); return $women;//[[XIAOXIN@163.COM]] PS:数据集合支持连缀操作,和数据库连缀一样;

    reject

    使用 reject()方法,可以获取条件之外的数据内容; $women = $users->reject(function ($user) { return $user->gender != '女'; }) ->map(function ($user) { return $user; }); //返回gender 女的数据

    contains

    //判断集合中是否包含指定的模型实例 return $users->contains(19);//1 return $users->contains(User::find(19));//1

    diff

    //返回不在集合中的所有模型 return $users->diff(User::whereIn('id', [19,20,21])->get());

    except

    //返回给定主键外的所有模型 return $users->except([19,20,21]);

    find

    //集合也有find 方法 return $users->find(19); //{"id":19,"username":"\u8721\u7b14\u5c0f\u65b0","password":"123","gender":"\u7537","email":"[xiaoxin@163.com]","price":"60.00","details":"123","uid":1001,"status":-1,"list":"{\"id\": 19, \"uid\": 1010}","deleted_at":null,"created_at":"2016-06-27T16:45:26.000000Z","updated_at":"2016-06-27T16:45:26.000000Z","info":"\u8721\u7b14\u5c0f\u65b0-xiaoxin@163.com"}

    count

    //返回集合的数量 return $users->count(); //20

    modelKeys

    //返回所有模型的主键 return $users->modelKeys(); //[19,20,21,24,25,26,27,29,76,79,80,99,304,305,308,309,312,313,314,315]

    only

    //返回主键的所有模型 return $users->only([19,20,21]); ///这几个主键的模型

    unique

    //返回集合中的唯一模型 唯一数据 return $users->unique();
    Processed: 0.009, SQL: 9