如何在Laravel中使用jenssegers库在mongodb模型和mysql模型之间建立多对多关系

时间:2019-04-27 13:24:14

标签: laravel mongodb many-to-many jenssegers-mongodb

我正在尝试使用laravel 5.7中的jenssegers库在mongodb模型和我的sql模型之间建立多对多关系,我想从mongodb模型中使用具有自定义外键的sql表中的关系,是否有办法实现?我正在使用HybridRelations,但无法正常工作。 这是我的模特。

     //******* MongoDB Model   ******//     
<?php

    namespace App\Models;

    use Jenssegers\Mongodb\Eloquent\Model;

    class Attribute extends Model {
        // This will save us time
        protected $connection = 'mongodb';
        protected $collection = 'attributes';
        protected $appends = ['id'];



        // This will add timestamps
        protected $dates = ['start_date_at', 'online_date_at'];

        // This will add hybrid ManyToMany relation with Category Model
        public function categories(){
            return $this->belongsToMany('App\Models\Category' , null , 'attribute_code' , 'category_id');
        }

    }


     //******* Mysql Model   ******//
    <?php
    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Jenssegers\Mongodb\Eloquent\HybridRelations;
    class Category extends Model{
        use HybridRelations;
        use SoftDeletes;
        protected $connection = 'mysql';
        protected $dates = ['deleted_at'];
        public function attributes(){
            return $this->belongsToMany('App\Models\Attribute');
        }
    }

0 个答案:

没有答案