You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel 8中使用jessenger包关联MongoDB两个集合

使用jenssegers/laravel-mongodb关联MongoDB集合的方法

首先要确保你的模型继承自Jenssegers\Mongodb\Eloquent\Model,而非Laravel默认的Eloquent模型,这样才能启用MongoDB的关联功能。下面是几种常见关联场景的实现示例:

1. 一对一关联

比如users集合和profiles集合,一个用户对应唯一一份个人资料。

User 模型

<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model
{
    protected $connection = 'mongodb'; // 指定MongoDB连接
    protected $collection = 'users'; // 对应集合名

    public function profile()
    {
        return $this->hasOne(Profile::class, 'user_id');
    }
}

Profile 模型

<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Profile extends Model
{
    protected $connection = 'mongodb';
    protected $collection = 'profiles';

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

使用示例

// 获取用户及其关联的个人资料
$user = User::with('profile')->find('用户的MongoDB ID');
// 获取个人资料对应的用户
$profile = Profile::with('user')->find('资料的MongoDB ID');

2. 一对多关联

比如posts集合和comments集合,一篇文章可以有多条评论。

Post 模型

<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
    protected $connection = 'mongodb';
    protected $collection = 'posts';

    public function comments()
    {
        return $this->hasMany(Comment::class, 'post_id');
    }
}

Comment 模型

<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Comment extends Model
{
    protected $connection = 'mongodb';
    protected $collection = 'comments';

    public function post()
    {
        return $this->belongsTo(Post::class, 'post_id');
    }
}

使用示例

// 获取文章及其所有评论
$post = Post::with('comments')->find('文章的MongoDB ID');
// 获取评论所属的文章
$comment = Comment::with('post')->find('评论的MongoDB ID');

3. 多对多关联

比如users集合和roles集合,一个用户可以拥有多个角色,一个角色也可以对应多个用户,需要中间集合user_role存储关联关系。

User 模型

<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model
{
    protected $connection = 'mongodb';
    protected $collection = 'users';

    public function roles()
    {
        return $this->belongsToMany(Role::class, 'user_role', 'user_id', 'role_id');
    }
}

Role 模型

<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Role extends Model
{
    protected $connection = 'mongodb';
    protected $collection = 'roles';

    public function users()
    {
        return $this->belongsToMany(User::class, 'user_role', 'role_id', 'user_id');
    }
}

使用示例

// 获取用户的所有角色
$user = User::with('roles')->find('用户的MongoDB ID');
// 获取角色对应的所有用户
$role = Role::with('users')->find('角色的MongoDB ID');
// 给用户添加角色
$user->roles()->attach('角色ID');
// 移除用户的某个角色
$user->roles()->detach('角色ID');

注意事项

  • 确保集合中的外键字段(如user_id、post_id)类型与关联模型的_id一致,MongoDB默认是ObjectId类型,所以外键最好存储为ObjectId,不要用字符串。
  • 如果集合使用了自定义主键,需要在模型中设置protected $primaryKey = '你的自定义主键名';,关联时也要指定正确的外键参数。
  • 关联查询的用法和Laravel原生Eloquent基本一致,with()预加载、has()/whereHas()条件筛选等功能都能正常使用。

内容的提问来源于stack exchange,提问作者Gaurav Jadhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 03:05:08