mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7k stars 1.43k forks source link

belongsToMany MySQL model #2324

Open bazalt opened 3 years ago

bazalt commented 3 years ago

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?

vahidei commented 2 years ago

same issue

lbrutti commented 1 year ago

same too