Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/curder/custom-collection-for-laravel-eloquent-model
https://github.com/curder/custom-collection-for-laravel-eloquent-model
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/curder/custom-collection-for-laravel-eloquent-model
- Owner: curder
- Created: 2024-04-30T09:04:16.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2024-04-30T09:09:14.000Z (7 months ago)
- Last Synced: 2024-04-30T10:40:24.274Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 81.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 自定义 Eloquent 模型集合
[![Check & fix styling](https://github.com/curder/custom-collection-for-laravel-eloquent-model/actions/workflows/pint.yml/badge.svg?branch=master)](https://github.com/curder/custom-collection-for-laravel-eloquent-model/actions/workflows/pint.yml)
[![Test Laravel Github action](https://github.com/curder/custom-collection-for-laravel-eloquent-model/actions/workflows/run-test.yml/badge.svg?branch=master)](https://github.com/curder/custom-collection-for-laravel-eloquent-model/actions/workflows/run-test.yml)在 Laravel 查询数据的过程中,需要将查询后的数据进行一些处理,可以通过自定义模型集合的方式重构。
比如下面的代码逻辑:
```php
use App\Models\Posts;Post::query()
->where('published_at', '<=', now()->toDateTimeString())
->orderBy('published_at', 'desc')
->get()
->keyBy('id')
->map(function ($post) {
return $post->title;
});
```建议将 `where` 查询和 `orderBy` 排序重构到 Eloquent 模型中,使代码更加清晰。
```php
// 重构之前
Post::query()
->where('published_at', '<=', now()->toDateTimeString())
->orderBy('published_at', 'desc')
->get()// 重构后
Post::query()->published()->orderByMostRecent()->get();
```然后需要在模型中定义一些方法,方便在其他查询中公用它们:
```php
class Post extends Model
{
// ...
public function scopePublished(Builder $query): Builder
{
return $query->whereNotNull('published_at')
->where('published_at', '<=', now()->toDateTimeString());
}public function scopeOrderByMostRecent(Builder $query): Builder
{
return $query->orderByDesc('published_at');
}
}
```经过上面的重构,我们的查询语句变得更加清晰,但是发现在进行查询数据后,还使用了 `keyBy()` 和 `map()` 的集合方法。
```php
use App\Models\Posts;Post::query()
->published()
->orderByMostRecent()
->get()
->keyBy('id')
->map(function ($post) {
return $post->title;
});
```可以通过新建一个自定义的模型集合类来完成。
```php
keyBy($key)
->map(
fn(Post $post) => $post->getAttribute($value)
);
}
}
```然后在模型中指定这个自定义的集合,而不是使用默认的。
```php
use App\Models\Collections\PostCollection;class Post extends Model
{
// ...
public function newCollection(array $models = [])
{
return PostCollection::make($models);
}
}
```此时,查询语句可以重构成这样:
```php
Post::query()->published()->orderByMostRecent()->get()->toDropdown();
```