PHPackages                             thoth-pharaoh/base-model-repository - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. thoth-pharaoh/base-model-repository

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

thoth-pharaoh/base-model-repository
===================================

基礎抽象類別供專案使用

v2.0.2(3y ago)031MITPHPPHP ^8.0

Since Jan 12Pushed 3y ago1 watchersCompare

[ Source](https://github.com/castion2293/base-model-repository)[ Packagist](https://packagist.org/packages/thoth-pharaoh/base-model-repository)[ RSS](/packages/thoth-pharaoh-base-model-repository/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (11)Used By (0)

基礎抽象類別供專案使用
===========

[](#基礎抽象類別供專案使用)

版本匹配
----

[](#版本匹配)

Laravelpackage8.X1.X9.X2.XAbstractBaseModel
-----------------

[](#abstractbasemodel)

### 使用方法

[](#使用方法)

```
namespace App\Models;

use Pharaoh\BaseModelRepository\Models\AbstractBaseModel;

class Blog extends AbstractBaseModel
{
}
```

### 功能

[](#功能)

- 自動載入 HasFactory Trait
- 自動設定 $guard = \['id'\]
- 自動序列化 DataTime 格式 `Y-m-d H:i:s`

AbstractBaseRepository
----------------------

[](#abstractbaserepository)

### 使用方法

[](#使用方法-1)

```
namespace App\Repositories;

use App\Models\Blog;
use Pharaoh\BaseModelRepository\Repositories\AbstractBaseRepository;

class BlogRepository extends AbstractBaseRepository
{
    public function __construct(Blog $blog)
    {
        $this->model = $blog;
        $this->table = $this->model->getTable();
    }
}
```

### 取得所有資料

[](#取得所有資料)

```
$repository->getAll($fields, $eagerLoad);
```

參數說明類型範例預設$fields要搜尋的欄位array\['name', 'type'\]\['\*'\]$eagerLoad預載入關聯array\['post', 'post.comment'\]\[\]### 依搜尋條件取得資料(whereIn)

[](#依搜尋條件取得資料wherein)

```
$repository->getByWhereIn($whereField, $whereValue, $field, $eagerLoad);
```

參數說明類型範例預設$whereField搜尋條件欄位名稱string'name''id'$whereValue搜尋條件資訊array\[1, 2, 3\]\[\]$field要搜尋的欄位array\['id', 'name'\]\['\*'\]$eagerLoad預載入關聯array\['post', 'post.comment'\]\[\]### 取得單筆資料

[](#取得單筆資料)

```
$repository->find($id, $eagerLoad);
```

參數說明類型範例預設$idPK123$eagerLoad預載入關聯array\['post', 'post.comment'\]\[\]### 取得單筆資料(單一搜尋條件)

[](#取得單筆資料單一搜尋條件)

```
$repository->findByWhere($where, $field, $eagerLoad);
```

參數說明類型範例預設$where搜尋條件資訊array\['status', '=', 1\]$field要搜尋的欄位array\['id', 'name'\]\['\*'\]$eagerLoad預載入關聯array\['post', 'post.comment'\]\[\]完整範例

```
$repository->findByWhere([
    ['status', '=', '1'],
    ['subscribed', '', '1'],
]);
```

or

```
$repository->findByWhere([
    ['status' => '1'],
    ['subscribed' => '1'],
]);
```

### 取得單筆資料 從寫入資料庫取得

[](#取得單筆資料-從寫入資料庫取得)

```
$repository->findWriteConnect($id);
```

參數說明類型範例預設$idPK123### 取得單筆資料 從寫入資料庫取得 並加排他鎖

[](#取得單筆資料-從寫入資料庫取得-並加排他鎖)

```
$repository->findWriteConnectByLockForUpdate($id);
```

參數說明類型範例預設$idPK123### 尋找在某個欄位有重複的數值及個數

[](#尋找在某個欄位有重複的數值及個數)

```
$repository->findDuplicateValue($field, $where, $extraParameters, $havingCount)
```

參數說明類型範例預設$field要搜尋的欄位string'member\_id'$where搜尋條件資訊array\['status', '=', 1\]$havingCount取多少以上int51完整範例

```
$repository->findDuplicateValue('member_id', [
  ['start_at', '>', '2022-01-10 00:00:00'],
  ['end_at', '
