LARALL HASMANY关系没有与MongoDB数据库一起工作

问题描述 投票:0回答:1
hello我有Laravel Laravel框架10.48.28 和mongodb1.45.3

我有牵引收藏 权限 许可证_curds

<?php namespace App\Models; use MongoDB\Laravel\Eloquent\Model; use Illuminate\Database\Eloquent\Collection; use MongoDB\BSON\ObjectId; class Permission extends Model { protected $fillable = ['name']; public function permission_curds() { return $this->hasMany(Permission_curd::class, 'permission_id', '_id'); } }
这是我的persissino_curd模型

<?php namespace App\Models; use MongoDB\Laravel\Eloquent\Model; class Permission_curd extends Model { protected $fillable = [ 'permission_id', 'read', 'update', 'create', 'delete', 'restore', 'force_delete', 'model_id' ]; }
现在我在控制器中做到了

$permission = Permission::with('permission_curds')->first(); dd($permission->permission_curds);

我得到这个结果

enter image description here EmptyArray

这是我的许可收集

enter image description here 这是我的允许_curds收藏

enter image description here 您可以看到_id是有关系的对象 我尝试了这个

$permission_curds = Permission_curd::where('permission_id',Helper::object_id('67a71938dd8a9219ab0d632b'))->get(); dd($permission_curds);

我得到结果


不是空数组
如果我将许可证_id作为普通字符串存储
它的工作发现,但Chatgpt告诉我不要将外键存储为Noremal Text将其存储为Objectid
这是我的助手:: object_id()函数代码

use MongoDB\BSON\ObjectId; public static function object_id($id) { if ($id === null || $id instanceof ObjectId) { return $id; } return new ObjectId($id); } enter image description here 有人可以帮我 谢谢

MongoDB不本质地支持关系模型及其典型关系(例如JONINS),这些关系通常在基于SQL的数据库中使用,并由ORMS(对象相关映射器)处理。 MongoDB是一个NOSQL数据库,它使用以文档为导向的模型,其中数据存储在类似JSON的文档中。

中,关系通常以不同的方式进行管理,例如通过嵌入文档或使用它们之间的手动参考,但它没有ORM为关系数据库提供的自动关系管理功能。

php laravel database mongodb
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.