sinemah / coucheloquent
Laravel 的轻量级 CouchDB 包装器
0.0.1-rc
2022-08-28 13:43 UTC
Requires
- php: >=8.1
- laravel/framework: ^9.0
README
轻量级 CouchDB 包装器用于 Laravel。CouchEloquent 使用类似的语法,但与之没有依赖关系。使用 Mango Query 处理所有请求。
安装
composer require sinemah/coucheloquent
配置
在你的配置文件夹中创建一个名为 couchdb.php
的文件,或者从 vendor 中复制。
<?php return [ 'session' => env('COUCHDB_SESSION', 300), 'username' => env('COUCHDB_USER'), 'password' => env('COUCHDB_PASSWORD'), 'database' => env('COUCHDB_DATABASE', 'laravel'), 'url' => env('COUCHDB_URL', 'http://couchdb:5984'), ];
模型
通过扩展 Sinemah\CouchEloquent\Eloquent\Model
来扩展你的模型。就是这样。
<?php namespace App\Models; use Sinemah\CouchEloquent\Eloquent\Model; class Item extends Model { protected $casts = [ 'name' => 'json', ]; }
支持的方法(模型实例)
- count
- find
- save
- update
- delete
- get
- toQuery
- where
- orWhere
- whereNot
- whereIn
- whereNull
- whereHas
- whereBetween
- first
- last
- exists
支持的数据类型
- int
- real
- float
- double
- decimal
- string
- bool
- object
- array
- json
- collection
使用方法
创建文档
use Ramsey\Uuid\Uuid; $doc = Pet::create([ 'name' => [ 'first_name' => 'Chico', 'last_name' => 'Mr.' ], 'treatments' => [ [ 'id' => Uuid::uuid6(), 'random_int' => rand(0, 10), ] ], 'gender' => 'male', 'breed' => 'Mastin-Mix' ]);
更新文档
$pet = $pets->first(); $pet->name = [ 'first_name' => 'chico', 'last_name' => 'MR.' ]; $pet->save();
查询构建器
$pets = Pet::query() ->where('breed', 'Mastin-Mix') ->whereIn('breed', ['Mastin-Mix']) ->whereBetween('created_at.values.year', [2022, 2023]) ->where(function(Builder $query) { $query->orWhere('gender', 'male'); $query->orWhere('gender', 'female'); return $query; }) ->whereHas('treatments', function(Builder $query) { $query->where('random_int', 1); return $query; }) ->get();