Skip to content

belongsToMany relationship is not working #1970

Open
@vinothini3

Description

@vinothini3

I have two collections entities and login_contacts :

entities collection :

 `[_id] => MongoDB\BSON\ObjectId Object
     (
        [oid] => 5e53b1202f3ce0657c1a33e3
     )

    [designation] => Final Testing New Entity

    [contacts] => Array
        (
            [0] => Array
                (
                    [contact_id] => MongoDB\BSON\ObjectId Object
                        (
                            [oid] => 5e53b1b82f3ce0657b7765e4
                        )

                    [firstname] => Testing Mongo Object Id
                    [lastname] => asdasd
                )

            [1] => Array
                (
                    [contact_id] => MongoDB\BSON\ObjectId Object
                        (
                            [oid] => 5e53b1cc2f3ce0657c1a33e4
                        )

                    [firstname] => Login Contact
                    [lastname] => asdasdasd
                )
        )

)`

Here is login_contacts collection :

   `[_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5e53b1b82f3ce0657b7765e4
        )


    [email] => test@test.com
    [password] => $2y$10$PYlZutASwrywnaoOBdLjHex6VJWKQi/wUkJ2HP5gptKyvl4vWv/ci`

So whenever i create contact i save details in login_contacts collections and then i save the same data under particular entities collection.

I want to get all the contacts under the particular entities and also the related data from login_contacts collection.

Im tried like this in my Entity (for entities collection) model:

public function contacts() { return $this->belongsToMany(LoginContact::class, null, '_id', 'contacts.contact_id'); }

Im trying to get it like this in my controller :

$data = Entity::with('contacts')->where('_id', '5e53b1202f3ce0657c1a33e3') ->where('d_delete', null) ->first();

But its always returning empty for the related login_contact datas

[relations:protected] => Array
(
[contacts] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions