Laravel 5.4 with mongodb:如何使用引用id查询来自多个集合的数据

时间:2017-05-19 09:05:11

标签: php mongodb laravel-5.4 jenssegers-mongodb

以下是我与

合作的作品集

费用包收集

{
    "_id": ObjectId("591abc701c9dfa0a6e7608e6"),
    "fee_package_name": "abc",
    "fee_structure": [{
            "fee_name": "Fee1",
            "fee_amount": 1550
        },
        {
            "fee_name": "Fee2",
            "fee_amount": 1750
        },
        {
            "fee_name": "Fee3",
            "fee_amount": 550
        }
    ],
    "total_fee_amount": 3850.0,
}

学费收取

{
    _id: ObjectId("585a98d7fc6c0468218267f9"),
    applied_fee_packages: [
        {
            fee_package_id: ObjectId("591abc701c9dfa0a6e7608e6"),
            applied_discount: 0
        },
        {
            fee_package_id: ObjectId("591abc701c9dfa0a6e7608e7"),
            applied_discount: 0
        }
    ]
}

Laravel:型号:FeePackage:

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class FeePackage extends Eloquent
{
    protected $collection = 'fee_packages';


    public function studentFee() {
        return $this->belongsTo('App\Models\StudentFee');
    }
}

Laravel:型号:StudentFee:

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class StudentFee extends Eloquent
{
    protected $collection = 'student_fees';    

    public function feePackages() {
        return $this->hasMany('App\Models\FeePackage');
    }
}

我试图定义hasMany关系,即学生费用有很多费用套餐,我试图找回一个学生,并且所有费用包都适用于他。

通过这些文件,我得到第一笔学费:

$feeCollection = StudentFee::with('feePackages')->first();

输出:

{
    _id: "585a98d7fc6c0468218267f9", 
    fee_packages: []
}

我只收到没有特定学生费用id的费用包详细信息的回复。

感谢任何解决此问题的指示。

提前致谢

0 个答案:

没有答案