phpmongokit/laravel-mongo-odm

PHPMongo Laravel 适配器

0.0.1 2018-02-05 00:13 UTC

This package is auto-updated.

Last update: 2024-08-28 01:55:41 UTC


README

Laravel 适配器,用于 PHPMongo ORM

Total Downloads

安装

添加 composer 依赖

composer require phpmongokit/laravel-mongo-odm

./config/app.php 中注册提供者

<?php

return [
    'providers' => [
        PHPMongoKit\ODM\Adapter\Laravel\MongoDbServiceProvider::class,
    ],
];

将你的连接配置添加到 ./config/mongodb.php

<?php

return [
    'connections' => [
        'connect1' => [
            'dsn' => 'mongodb://mongodb',
        ],
    ],
];

使用

从服务容器中获取连接池

<?php

use Psr\Container\ContainerInterface;
use Sokil\Mongo\ClientPool;

Route::get('/', function (ContainerInterface $container) {
    /** @var ClientPool $clientPool */
    $clientPool = $container->get(ClientPool::class);
    $connection = $clientPool->get('connect1'); // Instance of Sokil\Mongo\Client
});