Laravel-mongodb: ManyTo Many Relationship between MongoDB and Mysql

Created on 27 Apr 2019  路  3Comments  路  Source: jenssegers/laravel-mongodb

hi , thank you for your great library and the your big efforts ,
i'm trying to make many to many relationship between mongodb and my sql
here is my code
This is Attribute 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');
    }

}

and here is Categorey 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;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $connection = 'mysql';
    protected $dates = `['deleted_at'];`

    public function attributes(){
        return $this->belongsToMany('App\Models\Attribute');
    }

}
i'm trying to make sql table as a pivot table between attributes(which is mongoDB table) and categories (which is mysql table ) with custom foreign key from attributes , but this is not working any suggestion of how can i achieve this with your library ?

question

All 3 comments

Category has many attributes and attributes belongs to many categories. So, you attribute relation should be hasMany in Category Model and in category relation should be belongstoMany in Attribute Model.
Also, since many-to-many relationship have a pivot table, you need to define the pivot table in belongToMany relation.
Refer this
Eloquent Many-to-Many Relationships

In this case, on which database the pivot table should be created ? on mysql or mongodb ?

@indowebdeveloper, let's go to slack. You can find link here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanjay1688 picture sanjay1688  路  3Comments

viacheslavpleshkov picture viacheslavpleshkov  路  3Comments

naveedyasin picture naveedyasin  路  3Comments

BlakeGardner picture BlakeGardner  路  3Comments

tomartailored picture tomartailored  路  3Comments