Skip to content

[Bug fix] BelongToMany relation with ObjectId #3015

Open
@florianJacques

Description

@florianJacques
  • Laravel-mongodb Version: 4.5
  • PHP Version: 8.3
  • Database Driver & Version: MongoDB extension version => 1.18.0

Description:

When we use a BelongsToMany relationship with objectIf in foreignKey, this one are incorrectly cast.

Create Planet model:

<?php

declare(strict_types=1);

namespace App\Models\Test;

use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\BelongsToMany;

class Planet extends Model
{
    protected $connection = 'mwe_mongodb';
    protected $collection = 'TestPlanet';

    public function getIdAttribute($value = null)
    {
        return $value;
    }

    public function visitors(): BelongsToMany
    {
        return $this->belongsToMany(SpaceExplorer::class, null, 'planetId', 'visitorId');
    }
}

Create SpaceExplorerModel

<?php

declare(strict_types=1);

namespace App\Models\Test;

use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\BelongsToMany;

class SpaceExplorer extends Model
{
    protected $connection = 'mwe_mongodb';
    protected $collection = 'TestSpaceExplorer';

    public function getIdAttribute($value = null)
    {
        return $value;
    }

    public function planetsVisited(): BelongsToMany
    {
        return $this->belongsToMany(Planet::class, null, 'visitorIds', 'planetIds');
    }
}

And attach planet to spaceExplorer:

<?php

use App\Models\Test\Planet;
use App\Models\Test\SpaceExplorer;

$planetEarth = new Planet();
$planetEarth->name = 'Earth';
$planetEarth->save();
$planetJupiter = new Planet();
$planetJupiter->name = 'Jupiter';
$planetJupiter->save();
$explorerTanya = new SpaceExplorer();
$explorerTanya->name = 'Tanya Kirbuk';
$explorerTanya->save();
$explorerTanya->planetsVisited()->attach($planetEarth);
$explorerTanya->planetsVisited()->attach($planetJupiter);

Expected behaviour

The expected result

{
    "_id" : ObjectId("667e95f3759cbea7c80d1a24"),
    "name" : "Tanya Kirbuk",
    "updated_at" : ISODate("2024-06-28T10:52:35.735+0000"),
    "created_at" : ISODate("2024-06-28T10:52:35.735+0000"),
    "planetIds" : [
        ObjectId("667e95f3759cbea7c80d1a22"),
        ObjectId("667e95f3759cbea7c80d1a23")
    ]
}

Actual behaviour

The current result

{
    "_id" : ObjectId("667e967908ccc87ad000a294"),
    "name" : "Tanya Kirbuk",
    "updated_at" : ISODate("2024-06-28T10:54:49.690+0000"),
    "created_at" : ISODate("2024-06-28T10:54:49.690+0000"),
    "planetIds" : [
        {
            "oid" : "667e967908ccc87ad000a292"
        },
        {
            "oid" : "667e967908ccc87ad000a293"
        }
    ]
}
 

I created a Pull request to resolve the issue #3014

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions