Skip to content

Commit

Permalink
Add custom query builder for laravel eloquent model
Browse files Browse the repository at this point in the history
  • Loading branch information
curder committed Apr 30, 2024
1 parent 8237173 commit e8b41d6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ export default {
{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-collection-for-laravel-eloquent-model"},
{text: '自定义 Eloquent 模型查询构建器', link: "/others/custom-query-builder-for-laravel-eloquent-model"},
],
},
{
Expand Down
3 changes: 2 additions & 1 deletion docs/others/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- [自定义逻辑 Macro](macros.md)
- [使用 Alpine.js 滚动到验证错误](scroll-to-validation-error-in-laravel-using-alpinejs.md)
- [生成视图的 Artisan 命令](artisan-command-to-generate-views-in-laravel.md)
- [自定义 Eloquent 模型集合](custom-collection-for-laravel-eloquent-model.md)
- [自定义 Eloquent 模型集合](custom-collection-for-laravel-eloquent-model.md)
- [自定义 Eloquent 模型查询构建器](custom-query-builder-for-laravel-eloquent-model.md)
103 changes: 103 additions & 0 deletions docs/others/custom-query-builder-for-laravel-eloquent-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# 自定义模型查询构建器

在 Laravel 中,经常会遇到包含太多业务逻辑的模型。此时可以构建自己的查询生成器类,以使模型更加精简。

比如下面的模型逻辑:

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

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, string $column = 'published_at'): Builder
{
return $query->orderByDesc($column);
}
}
```

使用模型对数据进行查询比如:

```php
\App\Models\Post::published()->orderByMostRecent()->get();
```

当模型的查询越来越多的时候,可以通过自定义查询构建器类来重构它,使模型变得简洁。

## 自定义模型构建器类

模型构建器类需要继承自 `\Illuminate\Database\Eloquent\Builder` 类。

```php
<?php

namespace App\Models\Builders;

use Illuminate\Database\Eloquent\Builder;

class PostBuilder extends Builder
{
//
}
```

然后将模型中的 `scope` 查询语法糖方法迁移到自定义构建器中:

```php
<?php

namespace App\Models\Builders;

use Illuminate\Database\Eloquent\Builder;

class PostBuilder extends Builder
{
public function published(): Builder
{
return $this->whereNotNull('published_at')
->where('published_at', '<=', now()->toDateTimeString());
}

public function orderByMostRecent(string $column = 'published_at'): Builder
{
return $this->orderByDesc($column);
}
}
```

并在模型中使用自定义的构建器类:

```php
<?php

namespace App\Models;

use App\Models\Builders\PostBuilder;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
// ...
public function newEloquentBuilder($query): PostBuilder
{
return new PostBuilder($query);
}
}
```


## 相关链接

- [GitHub - Custom query builderfor laravel eloquent model](https://github.com/curder/custom-query-builder-for-laravel-eloquent-model)

0 comments on commit e8b41d6

Please sign in to comment.