Why laravel returns an empty array for a has many relationship?

hu7sy

I am using jenssegers/laravel-mongodb and i have schema is like below

Ticket Collection :

{
      "_id": ObjectId("5f32d9bb486e94459b6531c3"),
      "subject": "\"URGENT\" Non-Compliance In (Eastern Region)",
      "content": "abc",
      "user_team": "5f044199e40dfe4847056785",
      "team_ids": [
        "5f3012bbb7c2bc422e4da5a2"
      ],
      "organization_id": "5f74359c7dcc8f6fbb2b47e2"
}

Team Collection :

{
      "_id":ObjectId("5f3012bbb7c2bc422e4da5a2"),
      "name": "Medical Maintenance",
      "createTickets": true
}

Relationship in Ticket Model :

public function teams()
    {
         return $this->HasMany('App\Team', 'team_ids');
    }

Relationship in Team Model :

public function ticket()
    {
        return $this->belongsTo('App\Ticket');
    }

I am facing an issue to get data for teams relationship. It return an emtpry array.

Laravel version is 6.2 jenssegers/mongodb version is 3.6

mrhn

Your approach to the foreign key is wrong, when in the context of hasMany. Instead a single column called team_id should be on the ticket and then you can do the following.

public function teams()
{
     return $this->HasMany('App\Team', 'team_id');
}

Which would work if your ticket looks like so.

{
      "_id": ObjectId("5f32d9bb486e94459b6531c3"),
      "subject": "\"URGENT\" Non-Compliance In (Eastern Region)",
      "content": "abc",
      "user_team": "5f044199e40dfe4847056785",
      "team_id":"5f3012bbb7c2bc422e4da5a2"
      "organization_id": "5f74359c7dcc8f6fbb2b47e2"
}

Instead it looks like you are actually doing a many to many, because one team can have many tickets and reverse. This can be defined like so, this will probably add the data to both models, but I'm not an expert on Mongodb in Laravel.

public function teams()
{
    return $this->belongsToMany(
        Team::class, null, 'ticket_ids', 'team_ids'
    );
}

You can find all of this in the documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel Many to Many Relationship or One to Many Relationship

Laravel 5.6: Many-to-many relationship returns empty object

Fetching has many relationship data Laravel and using avg function

Laravel Eloquent Eager Loading Multiple Has Many Relationship

Laravel Many to Many returns empty collection

One To Many returns empty array (solved)

Laravel One to Many relationship not working - returns recursion

Rails has_many association returns empty array

Laravel count number of rows in has many relationship

Laravel 5 Eloquent Relationship - Has Many Though

Laravel 5 eloquent relationship returns empty or null but the code seems ok

has_many :through association returns empty array on one side and filled array on the other

Many to many relationship in Laravel

Many to Many relationship with Laravel

Many to many relationship returns empty set

Why is this function returns an empty array?

One to many resource relationship returns empty

Laravel many to many relationship with uuid returns always empty

Display one column from table in has many relationship in Laravel

Laravel many to many relationship collection empty

One To Many returns empty array

Laravel belongsTo relationship returns empty object

Why Laravel query returns empty array?

Laravel Filter Many to Many Relationship with whereIn array values

Laravel Join returns empty array

Laravel 8: Error when using pivot in an empty many to many relationship

Laravel: Order a model by "Has One Of Many" relationship

Rails many-to-many through relationship returning empty array

Laravel how to merge belongs to many relationship with has many relation