集合
Illuminate\Support\Collection 类提供了一个更具可读性和更便于处理数组数据的封装。
创建集合
$collection = collect([1, 2, 3]);
扩展集合
集合都是「可宏扩展」(macroable) 的,它允许你在执行时将其它方法添加到 Collection 类。
use Illuminate\Support\Str;
Collection::macro('toUpper', function () {
return $this->map(function ($value) {
return Str::upper($value);
});
});
$collection = collect(['first', 'second']);
$upper = $collection->toUpper();
// ['FIRST', 'SECOND']
常用方法
- contains:判断集合是否包含指定的集合项;
- count:统计集合内集合项的总数量;
- countBy:计算集合中每个值的出现次数;
- crossJoin:交叉连接指定数组或集合的值;
- dd:打印集合元素并中断脚本执行;
- diff:将集合与其它集合或者数组进行值的比较,然后返回原集合中存在而指定集合中不存在的值;
- dump:打印集合项;
- duplicates:从集合中检索并返回重复的值;
- each:循环集合项并将其传递到回调函数中;
- except;
- filter;
- first;
- firstWhere;
- flatMap;
- flatten;
- flip;
- forget;
- forPage;
- get;
- groupBy;
- has;
- implode;
- intersect;
- intersectByKeys;
- isEmpty;
- isNotEmpty;
- join;
- keyBy;
- keys;
- last;
- macro;
- make;
- map;
- mapInto;
- mapSpread;
- mapToGroups;
- mapWithKeys;
- max;
- median;
- merge;
- mergeRecursive;
- min;
- mode;
- nth;
- only;
- pad;
- partition;
- pipe;
- pluck;
- pop;
- prepend;
- pull;
- push;
- put;
- random;
- reduce;
- reject;
- replace;
- replaceRecursive;
- reverse;
- search;
- shift;
- shuffle;
- skip;
- slice;
- some;
- sort;
- sortBy;
- sortByDesc;
- sortKeys;
- sortKeysDesc;
- splice;
- split;
- sum;
- take;
- tap;
- times;
- toArray;
- toJson;
- transform;
- union;
- unique;
- uniqueStrict;
- unless;
- unlessEmpty;
- unlessNotEmpty;
- unwrap;
- values;
- when;
- whenEmpty;
- whenNotEmpty;
- where;
- whereStrict;
- whereBetween;
- whereIn;
- whereInStrict;
- whereInstanceOf;
- whereNotBetween;
- whereNotIn;
- whereNotInStrict;
- wrap;
- zip;
函数
Laravel 包含各种各样的全局 PHP 「辅助」函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数。
路径
- app_path:返回 app 目录的完整路径;
- base_path:返回项目根目录的完整路径;
- config_path:返回 config 目录的完整路径;
- database_path:返回 database 目录的完整路径;
- public_path:返回 public 目录的完整路径;
- resource_path:返回 resources 目录的完整路径;
- storage_path:返回 storage 目录的完整路径;
URLs
- action:为指定的控制器动作生成一个 URL;
- asset:为资源文件生成 URL;
- route:为给定的命名路由生成一个 URL ;
- secure_asset:使用 HTTPS 协议为资源文件生成 URL;
- secure_url:为给定的路径生成一个标准的 HTTPS URL;
- url:生成给定路径的标准 URL;
其他
- abort
- abort_if
- abort_unless
- app
- auth
- back
- bcrypt
- blank
- broadcast
- cache
- class_uses_recursive
- collect
- config
- cookie
- csrf_field
- csrf_token
- dd
- decrypt
- dispatch
- dispatch_now
- dump
- encrypt
- env
- event
- factory
- filled
- info
- logger
- method_field
- now
- old
- optional
- policy
- redirect
- report
- request
- rescue
- resolve
- response
- retry
- session
- tap
- throw_if
- throw_unless
- today
- trait_uses_recursive
- transform
- validator
- value
- view
- with