laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

belongsToMany MySQL model

Open bazalt opened this issue 2 years ago • 2 comments

  • Laravel-mongodb Version: 3.8.4
  • PHP Version: 8.0.3
  • Database Driver & Version: mongodb 1.18.0

Hello,

Some help would be appreciated to handle an hybrid MongoDB/MySQL relation. I have a MongoDB model Foo than contains this kind of documents:

[{
    _id: "doc1"
    foo: "bar",
    colors: [1,2]
},
{
    _id: "doc2"
    foo: "baz",
    colors: [3]
},
{
    _id: "doc3"
    foo: "baa",
    colors: [1,3]
}]

The colors property is an array of IDs referencing a classic MySQL Laravel model (I don't know if there's a better way to store this kind of data).

The Mongo-model Foo contains a colors() method, supposed to handle the relation with the MySQL-model Colors :

public function colors()
{
    return $this->belongsToMany(
        Color::class,
        null
    );
}

Which produce this error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app.color_foo' doesn't exist (SQL: select `colors`.*, `color_foo`.`product_id` as `pivot_foo_id`, `color_foo`.`color_id` as `pivot_color_id` from `colors` inner join `color_foo` on `colors`.`id` = `color_foo`.`color_id` where `color_foo`.`product_id` in (616d709341014ac3b728ceb7, 616d709341014ac3b728ceb8, 616d709341014ac3b728ceb9,[...])

How would you structure your MongoDB documents and/or adapt the Model in this case?

bazalt avatar Oct 18 '21 13:10 bazalt

same issue

vahidei avatar Oct 18 '22 11:10 vahidei

same too

lbrutti avatar Mar 02 '23 15:03 lbrutti