Laravel 的集合 Collection简介Illuminate\Support\Collection 类提供了一个更具可读性的、更便于处理数组数据的封装。具体例子看下面的代码。我们使用了 collect 函数从数组中创建新的集合实例,对其中的每个元素运行 strtoupper 函数之后再移除所有的空元素:$...
use App\Jobs\ImportContacts; use Illuminate\Support\Collection; /** * 执行任务。 * * @return void */ public function handle() { if ($this->batch()->cancelled()) { return; } $this->batch()->add(Collection::times(1000, function () { return new ImportContacts; })); }注意:你只能...
Illuminate\Support\Collection 类提供了一个更具可读性的、更便于处理数组数据的封装,具体例子看下面的代码。 我们使用了 collect 函数从数组中创建新的集合实例,对其中的每个元素运行 strtoupper 函数之后再移除所有的空元素 代码语言:javascript 代码运行次数:0 运行 AI代码解释$...
$value = $collection->get('name'); // taylor #可以选择性地传递默认值作为第二个参数: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('foo', 'default-value'); // default-value #甚至可以将回调函数当作默认值。如果指定的键不存在,就...
如果您想将经过验证的数据作为 collection 实例检索,您可以调用 collect 方法:$collection = $request->safe()->collect();处理错误信息通过Validator 实例调用 errors 方法,它会返回一个 Illuminate\Support\MessageBag 实例,该实例包含了各种可以很方便地处理错误信息的方法。并自动给所有视图提供 $errors 变量,也是 ...
We'll use the collect helper to create a new collection instance from the array, run the strtoupper function on each element, and then remove all empty elements:$collection = collect(['taylor', 'abigail', null])->map(function ($name) { return strtoupper($name); })->reject(function ($...
4$response->collect($key = null) : Illuminate\Support\Collection; 5$response->status() : int; 6$response->successful() : bool; 7$response->redirect(): bool; 8$response->failed() : bool; 9$response->clientError() : bool; 10$response->header($header) : string; 11$response->headers...
这个查找过程涉及到IlluminateSupportCollection组件,具体是partition方法。 总结 在这篇文章我们主要学习一下几个有关路由处理的相关知识: Laravel 中的路由如何被加载到项目中; 如何接收 HTTP 请求; 如何依据 HTTP 请求($request)查找所匹配的路由; 运行路由闭包或控制器方法。
* @return array|\Illuminate\Database\Eloquent\Collection|static[] */ public function find($id, $columns = array('*')); /** * @param $field * @param $value * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] ...
Laravel 提供了一些超赞的组件,在我看来,它是目前所有 Web 框架中提供组件支持最好的一个。...预览最长接触到使用集合的场景来自于研发人员使用 Eloquent 执行数据库查询,并从返回数据中使用 foreach 语句遍历获取模型集合。...扩展集合(Extending Collections) Collection 类,同其它 Laravel 组件一样,支持宏(macroable...