as247/wp-eloquent

WordPress 的 Eloquent ORM。

v2.1.9 2023-11-21 05:49 UTC

README

此包是从 Laravel 8.9 提取的。

WpEloquent 组件是 PHP 的完整数据库工具包,提供表达式查询构建器、ActiveRecord 风格的 ORM 和架构构建器。它目前支持 MySQL、Postgres、SQL Server 和 SQLite。

安装

composer require as247/wp-eloquent

使用说明

首先,使用连接启动应用程序。

使用 $wpdb 连接

use As247\WpEloquent\Application;

Application::bootWp();

使用单独的连接

use As247\WpEloquent\Application;

Application::boot([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

应用程序启动后,您可以如此使用它

使用查询构建器

use As247\WpEloquent\Support\Facades\DB;
$users = DB::table('users')->where('votes', '>', 100)->get();

其他核心方法可以直接从 Capsule 以与 DB 门面相同的方式访问

use As247\WpEloquent\Support\Facades\DB;
$results = DB::select('select * from users where id = ?', [1]);

使用架构构建器

use As247\WpEloquent\Support\Facades\Schema;
Schema::create('users', function ($table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});

使用 Eloquent ORM

class User extends As247\WpEloquent\Database\Eloquent\Model {}

$users = User::where('votes', '>', 1)->get();

有关使用此库提供的各种数据库设施的进一步文档,请参阅 Laravel 框架文档

版本 1.0

您正在寻找 v1.x 版本?请在此处查看 https://github.com/as247/wp-eloquent/tree/1.x