laravel-mongodb: hasMany or hasOne doesnt work without getIdAttribute

This issue has been around, can you guys provide a documentation how to properly make sure things work in hasMany and hasOne

for now using this to make sure hasMany and hasOne work

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

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (15 by maintainers)

Most upvoted comments

I agree that using ObjectIds is a good practice for foreign keys. I tried to implement it, but found issues with how the parent Eloquent classes work with foreign keys. They assume every primary/foreign key can be represented as a string. This need in-deep investigation to find how we can implement it in a way that we can maintain in a long-term, without breaking existing application that rely on string foreign keys.

There is no plan for now.

Hi @masterbater, we have a test suite that cover relationships, including hasMany and hasOne. Could you be more precise about what is the issue: steps to reproduce, expected result and observed result.

I think you dont have a test for this. For data consistency we always save the real objectid not a string representation on the collection.

This Fails hasMany


   $user = new User;
    $user->name = 'Test';
    $user->email = 'test@gmail';
    $user->save();
    $readings = new Reading;
    $readings->patientId = new ObjectId($user->id);
    $readings->value = 120;
    $readings->unit = 'mmHg';
    $readings->save();
    return $user->readings;

This make the relationship hasMany work


   $user = new User;
    $user->name = 'Test';
    $user->email = 'test@gmail';
    $user->save();
    $readings = new Reading;
    $readings->patientId = $user->id;
    $readings->value = 120;
    $readings->unit = 'mmHg';
    $readings->save();
    return $user->readings;

User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use MongoDB\Laravel\Eloquent\Model;

class User extends Model
{
    use HasFactory, Notifiable;
    protected $connection = 'mongodb';
    protected $collection = 'users';
    const CREATED_AT = 'createdAt';
    const UPDATED_AT = 'updatedAt';
    public static $snakeAttributes = false;

    public function readings()
    {
        return $this->hasMany(Reading::class, 'patientId');
    }
}

Hopefully it could be fix, this I think should be important especially for some people having different codebases.

@masterbater They’re the same as the Eloquent’s hasMany or hasOne. You can pass your custom keys as parameters.

For hasOne:

https://github.com/mongodb/laravel-mongodb/blob/963d01f1af6b1d54082399972523bde4abdf0788/src/Eloquent/HybridRelations.php#L46

For hasMany:

https://github.com/mongodb/laravel-mongodb/blob/963d01f1af6b1d54082399972523bde4abdf0788/src/Eloquent/HybridRelations.php#L102

The issue that you referred to, is solved by now and no longer exists. I think you are using an older version of this package. Please specify the version you are using.

version 4.1.3, I did it work but needs to add getIdAttribute to each models. I pass in custom keys since it convert camelCase keys into snake_keys