PHPackages                             goenitz/simple-model - 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. [Database &amp; ORM](/categories/database)
4. /
5. goenitz/simple-model

AbandonedLibrary[Database &amp; ORM](/categories/database)

goenitz/simple-model
====================

A laravel eloquent-like mysql class

0.01(9y ago)112MITPHPPHP &gt;=7.0

Since Mar 18Pushed 9y ago1 watchersCompare

[ Source](https://github.com/tianyirenjian/simpleModel)[ Packagist](https://packagist.org/packages/goenitz/simple-model)[ Docs](https://github.com/tianyirenjian/simpleModel)[ RSS](/packages/goenitz-simple-model/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

SimpleModel
===========

[](#simplemodel)

仿Laravel Eloquent样式的mysql数据库操作类库。

安装方法

```
composer require goenitz/simple-model
```

配置数据库信息

```
require 'vendor/autoload.php';

\Goenitz\SimpleModel\Model::connect([
    'host' => '127.0.0.1',
    'port' => 3306,
    'database' => 'yourdatabasename',
    'username' => 'yourdatabaseusername',
    'password' => 'yourpassword'
]);
```

示例
--

[](#示例)

假设有一个表，有id， title, content三个字段。

首先创建一个类

```
namespace App;

use Goenitz\SimpleModel\Model;

class Article extends Model
{
    //protected $table = 'articles'; // 表名, 多数情况下会自动使用复数形式
    protected $fillable = ['title', 'content']; // 可以插入的字段
    //protected $identifier = 'id'; // 自增主键，默认为id
    //protected $hidden = ['content']; // 转换json, array 时隐藏的字段

    //用于设置属性,一般情况下不需要
    //protected function setTitleAttribute($value)
    //{
    //    $this->attributes['title'] = strtoupper($value);
    //}

    //获取属性,一般情况下不需要
    //protected function getTitleAttribute()
    //{
    //    return $this->attributes['title'] . 'xyz';
    //}
}
```

现在就可以使用了。

#### 添加数据

[](#添加数据)

```
use App\Article;

Article::create([
    'title' => 'test',
    'content' => 'just a test'
]);
```

或者

```
$article = new Article();
$article->title = 'another test';
$article->content = 'another content';
$article->save();
```

也可心一次添加多个

```
$newArticles = Article::createMany([
    [
        'title' => 'test1',
        'content' => 'content1'
    ],
    [
        'title' => 'test2',
        'content' => 'content2'
    ]
]);
dd($newArticles);
```

#### 查询数据

[](#查询数据)

查询第一条

```
$article = Article::first();

echo $article->title . '';
echo $article['title'] . '';
echo $article->content . '';
echo $article['content'] . '';
```

查询所有

```
$articles = Article::all();
```

按主键查询

```
$article = Article::find(1);
```

使用limit, skip , orderBy 查询

```
$articles = Article::limit(10)->skip(10)->orderBy('id', 'desc')->get();
dump($articles);
```

使用select 来确定查询的列

```
$article = Article::select(['id', 'title'])->first();
```

也可以在get方法传入参数来使用

```
$article = Article::where('id', 50)->get(['title', 'content']);
```

条件查询

```
$articles = Article::where('id', '50')->get();
$articles = Article::where(['id', '50'])->get();

$articles = Article::where('id', '>', '50')->get();
$articles = Article::where(['id', '>', '50'])->get();

$articles = Article::where([
    ['id', '>', '50'],
    ['title', '', 'test'],
])->get();

$articles = Article::where('id', '>', '50')->orWhere('id', '', '50')->limit(10)->skip(10)->orderBy('id')->get();

//还可以闭包使用，但是不支持闭包内部再使用闭包
$articles = Article::where(function ($query) {
    $query->where(['id', '>', '50']);
    $query->where(['title', '', 'test']);
})->get();

$articles = Article::where(function ($query) {
    $query->where([
        ['id', '>', 50],
        ['title', '', 'test']
    ]);
})->get()

$articles = Article::where(function ($query) {
    $query->where(['id', '>', 50]);
    $query->orWhere(['title', '', 'test']);
})->get()

$articles = Article::whereIn('id', [10, 15, 20])->get();

$articles = Article::whereIn('id', [10, 15, 20], true)->get(); // not in
```

**运行前可以把get改为toSql来查看sql语句，避免错误发生**

**whereIn 目前不建议和其它where连用，会产生不可预知的错误**

#### 修改数据

[](#修改数据)

```
$article = Article::find(5);
$article->title = 'updated';
$article->save();
```

或者

```
$article = Article::find(5);
$article->update([
    'title' => 'foo',
    'content' => 'bar'
]);
```

批量修改

```
Article::where('title', 'test')->update(['title', 'foo']);
Article::whereIn('id', [5, 10, 15])->update(['title', 'foo']);
```

#### 删除数据

[](#删除数据)

删除一条

```
$article = Article::first();
$article->delete();
```

通过id删除多条

```
Article::destroy([5, 10, 15]);
//或
Article::destroy(5, 10, 15);
```

通过条件删除

```
Article::where('id', '18')->delete();
Article::whereIn('id', [19, 20, 21])->delete();
```

#### 格式化数据

[](#格式化数据)

Model类实现了Jsonable和Arrayable接口，在Article类中设置 `protected $hidden = ['content']` 可设置要隐藏的列

```
Article::find(30)->toJson();
//{"id":"30","title":"foo"}

Article::find(30)->toArray();
/*[
    "id" => "30",
    "title" => "foo"
]*/
```

对于通过where等查出来的多条数据也一样适用

写在最后
----

[](#写在最后)

还有一些操作未实现或者不能通过以上方法完成，可以直接作用PDO来完成

```
$connection = \Goenitz\SimpleModel\Model::$connection;
// do everything you want.
```

这就是PDO对象了。

因为本类库使用了 illuminate/support 和symfony/var-dumper。所以你可以使用任何它们的方法。 具体可参考  .

还有...
-----

[](#还有)

#### todo

[](#todo)

- toJson, toArray 的类型问题
- where, orWhere, whereIn 方法的优化和修复
- 表之间的关联

#### change logs

[](#change-logs)

0.01 初始版本

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3391d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/01b8c119a491e1583e732fef4d8bfea2b36398d8081895b78987ab2248588868?d=identicon)[Goenitz](/maintainers/Goenitz)

---

Top Contributors

[![tianyirenjian](https://avatars.githubusercontent.com/u/3616700?v=4)](https://github.com/tianyirenjian "tianyirenjian (6 commits)")

---

Tags

laravelmodeleloquent

### Embed Badge

![Health badge](/badges/goenitz-simple-model/health.svg)

```
[![Health](https://phpackages.com/badges/goenitz-simple-model/health.svg)](https://phpackages.com/packages/goenitz-simple-model)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M91](/packages/mongodb-laravel-mongodb)[craftcms/cms

Craft CMS

3.6k3.6M2.9k](/packages/craftcms-cms)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k5.1M31](/packages/tucker-eric-eloquentfilter)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

592452.8k2](/packages/spiritix-lada-cache)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121134.8k](/packages/highsolutions-eloquent-sequence)[spatie/laravel-flare

Send Laravel errors to Flare

111.4M6](/packages/spatie-laravel-flare)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
