rosamarsky/laravel-doctrine-odm

Simple Laravel Doctrine ODM adapter

Installs: 378

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/rosamarsky/laravel-doctrine-odm

3.0.2 2025-11-20 12:40 UTC

This package is auto-updated.

Last update: 2025-11-20 12:40:39 UTC


README

A simple Doctrine ODM adapter for Laravel that supports attribute and XML mapping for MongoDB.

Features

  • Supports PHP 8 attributes and XML mappings.
  • Works with Doctrine ODM 3.0+.
  • Laravel service provider for easy integration.
  • Registers custom Carbon type automatically.

Installation

Install via Composer:

composer require rosamarsky/laravel-doctrine-odm

The package auto-registers the service provider, so no manual registration is required.

Configuration

Publish the config file:

php artisan vendor:publish --provider="Rosamarsky\LaravelDoctrineOdm\ServiceProvider" --tag=config

This will create config/doctrine-odm.php.

Set your MongoDB credentials in .env or directly in the config:

MONGO_HOST=127.0.0.1
MONGO_PORT=27017
MONGO_DB=your_database
MONGO_USER=
MONGO_PASS=

Usage

Define Documents

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Carbon\Carbon;

#[ODM\Document(collection: "users")]
class User
{
    #[ODM\Id]
    private string $id;

    #[ODM\Field(type: "string")]
    private string $name;

    #[ODM\Field(type: "string")]
    private string $email;

    #[ODM\Field(type: "carbon")]
    private Carbon $createdAt;

    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
        $this->createdAt = new Carbon();
    }
}

XML mapping is also supported if you configure driver => 'xml' and put your XML files in metadata_dir.

Using DocumentManager

use \Doctrine\ODM\MongoDB\DocumentManager;

class UserController extends AbstractController
{
    public function __construct(private readonly DocumentManager $manager) {}

    public function store(Request $request): User
    {
        $user = new User('Roman Samarsky', 'rosamarsky@gmail.com');

        $this->manager->persist($user);
        $this->manager->flush();

        return $user;
    }
}

License

MIT © Roman Samarsky