-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hasMany or hasOne doesnt work without getIdAttribute #2753
Comments
Hi @masterbater, we have a test suite that cover relationships, including |
I found the workaround because of this issue #1902 (comment) I believe the cause of the issue since I am using an existing db that is saved base on mongoose or prisma, foreign keys also get converted into snake_case instead of camelCase. This are the gist of my collection
|
@masterbater They're the same as the Eloquent's For
For
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 |
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": [
"laravel",
"framework"
],
"license": "MIT",
"require": {
"php": "^8.1",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/octane": "^2.3",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8",
"mongodb/laravel-mongodb": "^4.1",
"mongodb/mongodb": "^1.17"
}, |
Could you please share your models and their relationships? |
Ill try to provide a fresh laravel project and mongodump file later. I will close this if I cant replicate it thanks for replying appreciate it |
No sweat buddy❤️ |
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. |
I don't understand why there is a default attribute mutator on the id key. 👨💻 |
Sorry for the delay. I will work on this whenever I find some free time. |
@GromNaN Sorry for pinging you directly is there any chance you can support relationship that is natively an objectid? Was this already been resolved? |
I agree that using ObjectIds is a good practice for foreign keys. |
Hello, do we have any news on this subject? |
There is no plan for now. |
I'm also overriding getIdAttribute() and on top of that I do this in model:
I do this for some relationships when doing $model->refresh(). Maybe this could also help you |
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
The text was updated successfully, but these errors were encountered: