1

I am trying to use a belongsToMany relation, in the following way: Model with belongsToMany relation:

namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class Notification extends \Jenssegers\Mongodb\Eloquent\Model
{
    use Notifiable, HasApiTokens;
    protected $collection = 'myDb.notifications';
    protected $fillable = [ ];
    protected $hidden = [  ];

    /**
    * List of involved users
    */
    public function participants()
    {
      return $this->belongsToMany(User::class);
    }
}

Creating a model:

$notification=new Notification();
....
$notification->participants()->attach(["5b13fb4393782d5e9010835d","5b13146f93782d29254c8cb2"]);
$notification->save();

Gives:

{
    ......
    "user_ids": [
        "5b13fb4393782d5e9010835d",
        "5b13146f93782d29254c8cb2"
    ],
    "updated_at": "2018-06-07 12:32:19",
    "created_at": "2018-06-07 12:32:19",
    "_id": "5b1925d393782d24b4514a16"
}

So ids are attached correctly, however, when I try to eager-load them with:

Notification::where('_id','=','5b1925d393782d24b4514a16')->with(['participants'])->get()

I get an empty array for the relation:

[
    {
        "_id": "5b1925d393782d24b4514a16",
        ....
        "updated_at": "2018-06-07 12:32:19",
        "created_at": "2018-06-07 12:32:19",
        "participants": []
    }
]

I have also verified that the given users actually exist, and should be "loadable", using:

User::whereIn('_id',["5b13fb4393782d5e9010835d","5b13146f93782d29254c8cb2"])->get()

Which gives the two users as expected....

1
  • Did you solve this? Commented Nov 23, 2018 at 12:29

2 Answers 2

1

Apparently we need two-way relationship, i.e. we need ids in BOTH tables.
We also need to set keys manually (or perhaps not, but I'm not sure what's right convention)

class Notification extends ...
{
    public function participants()
    {
      // NOTE: we set keys manually here:
      return $this->belongsToMany(User::class, null, 'notification_ids', 'user_ids');
    }
}

class User extends ...
{
    public function notifications()
    {
      // NOTE: we set keys manually here:
      return $this->belongsToMany(Notification::class, null, 'user_ids', 'notification_ids');
    }
}

Considering objects have following properties (fields):

User:

{
   ...
   notification_ids: []
}

Notification:

{
   ...
   user_ids: []
}

(both array of strings (ids))

Using attach should update both tables properly, for example:

$user->notifications()->attach($notification->id)

Sign up to request clarification or add additional context in comments.

Comments

0

I think there is a problem with the relation, can you try something like :

return $this->belongsToMany('App\User');

1 Comment

I have tried... gives the same result... I also tried specifying the attribute with ->belongsToMany(User::class,null,'user_ids'); but still empty array...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.