Skip to content

Commit

Permalink
Add after and before method for collection
Browse files Browse the repository at this point in the history
  • Loading branch information
curder committed Jun 20, 2024
1 parent 9f90d36 commit bfa9633
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 36 deletions.
74 changes: 38 additions & 36 deletions docs/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
'/collections/': [
"/collections/": [
{
text: '一些示例',
text: "一些示例",
items: [
{text: "计算 GitHub 事件得分总和", link: "/collections/examples/format-github-events-score"},
{
Expand All @@ -17,19 +17,21 @@ export default {
],
},
{
text: '集合方法',
text: "集合方法",
items: [
{text: "after", link: "collections/after"},
{text: "all", link: "/collections/all"},
{text: "average", link: "/collections/avg"},
{text: "avg", link: "/collections/avg"},
{text: "before", link: "/collections/before"},
{text: "chunk", link: "/collections/chunk"},
{text: "chunkWhile", link: "/collections/chunkWhile"},
{text: "collapse", link: "/collections/collapse"},
{text: "collect", link: "/collections/collect"},
{text: "combine", link: "/collections/combine"},
{text: "concat", link: "/collections/concat"},
{text: "contains", link: "/collections/contains"},
{text: 'containsOneItem', link: '/collections/containsOneItem'},
{text: "containsOneItem", link: "/collections/containsOneItem"},
{text: "containsStrict", link: "/collections/containsStrict"},
{text: "count", link: "/collections/count"},
{text: "countBy", link: "/collections/countBy"},
Expand Down Expand Up @@ -74,7 +76,7 @@ export default {
{text: "last", link: "/collections/last"},
{text: "lazy", link: "/collections/lazy"},
{text: "macro", link: "/collections/macro"},
{text: 'make', link: '/collections/make'},
{text: "make", link: "/collections/make"},
{text: "map", link: "/collections/map"},
{text: "mapInto", link: "/collections/mapInto"},
{text: "mapSpread", link: "/collections/mapSpread"},
Expand Down Expand Up @@ -163,9 +165,9 @@ export default {
],
},
],
'/model/': [
"/model/": [
{
text: '关联关系',
text: "关联关系",
items: [
{text: "一对一", link: "/model/relationships/01-has-one"},
{text: "一对多", link: "/model/relationships/02-has-many"},
Expand All @@ -180,7 +182,7 @@ export default {
],
},
{
text: '基础',
text: "基础",
items: [
{text: "模型缓存", link: "/model/laravel-model-caching"},
{text: "模型事件", link: "/model/laravel-model-events"},
Expand All @@ -189,38 +191,38 @@ export default {
],
},
],
'/packages/': [
"/packages/": [
{
text: 'Laravel',
text: "Laravel",
items: [
{text: "Pail", link: "/packages/laravel/pail"},
{text: "Pint", link: "/packages/laravel/pint"},
{text: "Prompts", link: "/packages/laravel/prompts"},
]
},
{
text: 'Spatie',
text: "Spatie",
items: [
{text: "Login Link", link: "/packages/spatie/laravel-login-link"},
{text: "Backup", link: "/packages/spatie/laravel-backup"},
{text: "Data", link: "/packages/spatie/laravel-data"},
]
},
{
text: 'Others',
text: "Others",
items: [
{text: "Sushi 🍣", link: "/packages/others/sushi"},
{text: "Livewire Missing Assertions", link: "/packages/others/missing-livewire-assertions"},
{text: 'Saloon', link: '/packages/others/saloon'},
{text: "Eloquent 查询缓存", 'link':'/packages/others/eloquent-query-cache'},
{text: "Saloon", link: "/packages/others/saloon"},
{text: "Eloquent 查询缓存", "link":"/packages/others/eloquent-query-cache"},
],
},
],
'/partials/': [
"/partials/": [
{
text: "",
items: [
{text: 'Migrations 迁移', link: "/partials/migrations"},
{text: "Migrations 迁移", link: "/partials/migrations"},
{text: "Model 模型", link: "/partials/model"},
{text: "Model Relations 模型关系", link: "/partials/model-relations"},
{text: "Views 视图", link: "/partials/views"},
Expand All @@ -232,22 +234,22 @@ export default {
],
}
],
'/tips/': [
"/tips/": [
{
text: '',
text: "",
items: [
{text: "DB 模型和 Eloquent", link: "/tips/db-models-and-eloquent"},
{text: "优化 Factory 数据库批量插入", link: '/tips/optimize-factory-database-batch-insert'},
{text: "重用环境变量值", link: '/tips/reuse-the-environment-variable-value'},
{text: "优化 Factory 数据库批量插入", link: "/tips/optimize-factory-database-batch-insert"},
{text: "重用环境变量值", link: "/tips/reuse-the-environment-variable-value"},
{
text: "使用 Laravel simple-excel 处理大型 CSV 文件",
link: '/tips/process-large-csv-files-with-laravel-using-simple-excel'
link: "/tips/process-large-csv-files-with-laravel-using-simple-excel"
},
{text: "Laravel 模块化开发", link: 'tips/modular-laravel'}
{text: "Laravel 模块化开发", link: "tips/modular-laravel"}
]
}
],
'/others/': [
"/others/": [
{
items: [
{text: "Laravel 11 新特性", link: "/others/whats-new-in-laravel-11"},
Expand All @@ -256,24 +258,24 @@ export default {
{text: "自定义异常", link: "/others/using-custom-exceptions-to-write-better-and-clearer-code"},
{text: "使用 pest 测试 Laravel API", link: "/others/unit-test-laravel-api-pest-framework.md"},
{text: "自定义逻辑 Macros", link: "/others/macros.md"},
{text: '使用 Alpine.js 滚动到验证错误', link: "/others/scroll-to-validation-error-in-laravel-using-alpinejs"},
{text: '生成视图的 Artisan 命令', link: "/others/artisan-command-to-generate-views-in-laravel"},
{text: '自定义 Eloquent 模型集合', link: "/others/custom-collection-for-laravel-eloquent-model"},
{text: '自定义 Eloquent 模型查询构建器', link: "/others/custom-query-builder-for-laravel-eloquent-model"},
{text: "使用 Alpine.js 滚动到验证错误", link: "/others/scroll-to-validation-error-in-laravel-using-alpinejs"},
{text: "生成视图的 Artisan 命令", link: "/others/artisan-command-to-generate-views-in-laravel"},
{text: "自定义 Eloquent 模型集合", link: "/others/custom-collection-for-laravel-eloquent-model"},
{text: "自定义 Eloquent 模型查询构建器", link: "/others/custom-query-builder-for-laravel-eloquent-model"},
],
},
{
text: '工具助手 Helpers',
text: "工具助手 Helpers",
items: [
{text: "数组 Arrays", link: '/others/helpers/arrays'},
{text: "数组 Arrays", link: "/others/helpers/arrays"},
{text: "Fluent 类", link: "/others/helpers/fluent"},
{text: "字符串 Strings", link: '/others/helpers/strings'},
{text: "字符串 Stringable", link: '/others/helpers/stringable'},
{text: "数字 Numbers", link: '/others/helpers/numbers'},
{text: "暂停执行 Sleep", link: '/others/helpers/sleep'},
{text: "路径 Paths", link: '/others/helpers/paths'},
{text: "网址 URLs", link: '/others/helpers/urls'},
{text: "日期时间 Carbon",link: '/others/helpers/carbon'},
{text: "字符串 Strings", link: "/others/helpers/strings"},
{text: "字符串 Stringable", link: "/others/helpers/stringable"},
{text: "数字 Numbers", link: "/others/helpers/numbers"},
{text: "暂停执行 Sleep", link: "/others/helpers/sleep"},
{text: "路径 Paths", link: "/others/helpers/paths"},
{text: "网址 URLs", link: "/others/helpers/urls"},
{text: "日期时间 Carbon",link: "/others/helpers/carbon"},
]
}
],
Expand Down
22 changes: 22 additions & 0 deletions docs/collections/after.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# after

> 获取给定项目之后的项目,如果给定的项目处于最后一个或不存在,则返回 `null`
```php
$tasks = collect([
['id' => 1, 'name' => '设计站点首页'],
['id' => 2, 'name' => '开发站点首页'],
['id' => 3, 'name' => '给首页填充内容'],
['id' => 4, 'name' => '设计站点关于我们页面'],
['id' => 5, 'name' => '开发站点关于我们页面'],
]);

$tasks->after(fn($task) => $task['name'] === '给首页填充内容'); // ["id" => 4, "name" => "设计站点关于我们页面"]

$tasks->before(fn (array $task) => $task['name'] === '开发站点关于我们页面'); // null
$tasks->before(fn (array $task) => $task['name'] === 'unknown'); // null
```

## 关联方法

- [before](before.md)
21 changes: 21 additions & 0 deletions docs/collections/before.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# before

> 获取给定项目之前的项目,如果给定的项目处于第一个或不存在,则返回 `null`
```php
$tasks = collect([
['id' => 1, 'name' => '设计站点首页'],
['id' => 2, 'name' => '开发站点首页'],
['id' => 3, 'name' => '给首页填充内容'],
['id' => 4, 'name' => '设计站点关于我们页面'],
['id' => 5, 'name' => '开发站点关于我们页面'],
]);

$tasks->before(fn ($task) => $task['name'] === '给首页填充内容'); // ["id" => 2, "name" => "开发站点首页"]

$tasks->before(fn (array $task) => $task['name'] === '设计站点首页'); // null
$tasks->before(fn (array $task) => $task['name'] === 'unknown'); // null
```
## 关联方法

- [after](after.md)
2 changes: 2 additions & 0 deletions docs/collections/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ collect([1, 2, 3]);

| 方法名 | 简单说明 |
|:----------------------------------------|:----------------------------------------------------------------------------------|
| [after](after.md) | 获取给定项目之后的项目 |
| [all](all.md) | 返回该集合表示的底层**数组** |
| [average](avg.md) | 方法`avg()`的别名 |
| [avg](avg.md) | 返回给定键的**平均值** |
| [before](before.md) | 获取给定项目之前的项目 |
| [chunk](chunk.md) | 将集合拆分为多个指定大小的集合组 |
| [chunkWhile](chunkWhile.md) | 根据给定的回调将集合分成多个较小的集合 |
| [collapse](collapse.md) | 将数组集合折叠成单个集合 |
Expand Down

0 comments on commit bfa9633

Please sign in to comment.