Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eshban's avatar

jenssegers / laravel-mongodb throws Attempt to read property "users" on null

I am using ' jenssegers /laravel-mongodb-core' package for Laravel+MongoDB connectivity. I am facing issues in the implementation of 'Many to Many' relationship in a default Laravel project but I only receive the following error:

"Attempt to read property "users" on null"

I even tried it with custom foreign keys but I did not get desired results and always get error message. I also know that this package not supports pivot tables for Many to Many relationships.

Please guide that how fix this issue

I have two models (user, role) and below is the code of my Models:

USER MODEL

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;


class user extends Model
{
   // protected $connection = 'mongodb';
    protected $collection = 'users';


    public function roles(){
        return $this->belongsToMany(role::class,null,'role_id_custom');
    }
}

ROLE MODEL

<?php

namespace App\Models;

#use Illuminate\Database\Eloquent\Factories\HasFactory;
#use Illuminate\Database\Eloquent\Model;

use Jenssegers\Mongodb\Eloquent\Model;

class role extends Model
{

   // protected $connection = 'mongodb';
    protected $collection = 'roles';

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

HOME Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\user;
use App\Models\role;

class home extends Controller
{

    public function index()
    {
        $role = user::find(1)->roles;
        return $role;

       // $user = role::find(1)->users;
        //return $user;
    }
    //
}

SCHEMA of MONGODB

USERS
_id:
name:
role_id_custom: (This is a foreign key)

ROLES
_id:
role:
role_id_custom

Please guide where is the issue? Is this is a package issue?

Many Thanks

0 likes
5 replies
LaryAI's avatar
Level 58

The issue is that the role_id_custom foreign key is not defined in the role model. To fix this, add the role_id_custom foreign key to the role model's belongsToMany relationship method. Here's an updated version of the role model:

class role extends Model
{
    protected $collection = 'roles';

    public function users(){
        return $this->belongsToMany(user::class, null, null, null, 'role_id_custom');
    }
}

Note that the fourth parameter of the belongsToMany method is for the name of the pivot table, which is not needed in this case since the package uses a convention for naming pivot tables. The fifth parameter is for the name of the foreign key on the pivot table, which is set to role_id_custom to match the foreign key in the user model.

Also, make sure that the role_id_custom foreign key is set correctly in the users collection in MongoDB.

With these changes, the index method in the home controller should return the roles for the user with ID 1.

vincent15000's avatar

This error is not related to MongoDB.

It means that you are trying to read the users property on a null variable.

For example : $role->users.

vincent15000's avatar

@eshban What I mean in my example $role->users is that $role is null.

You are attempting to access the users variable from a null parent, that's like you were doing : null->users.

It's not necessarily the $role variable, it can be something else in your code.

eshban's avatar

Anyone had a chance to use Jenssegers package and use Many-Many Relationship?

Please or to participate in this conversation.