lumphp / laravel-database
Illuminate 数据库包。
v8.36.2
2021-09-20 22:41 UTC
Requires
- php: ^7.3|^8.0
- ext-json: *
- lumphp/laravel-collections: ^8.0
- lumphp/laravel-container: ^8.0
- lumphp/laravel-contracts: ^8.0
- lumphp/laravel-macroable: ^8.0
- lumphp/laravel-support: ^8.0
- symfony/console: ^5.1.4
Suggests
- doctrine/dbal: Required to rename columns and drop SQLite columns (^2.6|^3.0).
- fakerphp/faker: Required to use the eloquent factory builder (^1.9.1).
- lumphp/laravel-console: Required to use the database commands (^8.0).
- lumphp/laravel-events: Required to use the observers with Eloquent (^8.0).
- lumphp/laravel-filesystem: Required to use the migrations (^8.0).
- lumphp/laravel-pagination: Required to paginate the result set (^8.0).
- symfony/finder: Required to use Eloquent model factories (^5.1.4).
This package is auto-updated.
Last update: 2024-09-21 06:53:45 UTC
README
The Illuminate Database component is a full database toolkit for PHP, providing an expressive query builder, ActiveRecord style ORM, and schema builder. It currently supports MySQL, Postgres, SQL Server, and SQLite. It also serves as the database layer of the Laravel PHP framework.
使用说明
首先,创建一个新的 "Capsule" 管理实例。Capsule 的目标是使在 Laravel 框架之外使用库的配置尽可能简单。
use Illuminate\Database\Capsule\Manager as Capsule; $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]); // Set the event dispatcher used by Eloquent models... (optional) use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; $capsule->setEventDispatcher(new Dispatcher(new Container)); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent();
composer require "illuminate/events"
在需要使用 Eloquent 的观察者时是必需的。
一旦 Capsule 实例已注册,您可以使用它如下
使用查询构建器
$users = Capsule::table('users')->where('votes', '>', 100)->get();
其他核心方法可以直接从 Capsule 以与 DB 门面相同的方式访问
$results = Capsule::select('select * from users where id = ?', [1]);
使用模式构建器
Capsule::schema()->create('users', function ($table) { $table->increments('id'); $table->string('email')->unique(); $table->timestamps(); });
使用 Eloquent ORM
class User extends Illuminate\Database\Eloquent\Model {} $users = User::where('votes', '>', 1)->get();
有关使用本库提供的各种数据库功能的高级文档,请参阅 Laravel 框架文档。