1

When using jenssegers/laravel-mongodb, the belongsTo relationship properly always returns null, despite, when dumping, the value appearing. How can I get the belongsTo relationship?

I have two models, a post and a user. Where the post has a belongs to relationship with user. When I use $post->user, I always get null. Despite the fact that when dumping ($post) it clearly shows the user id!

User (Using the boiler plate laravel auth, except with MongoDB auth user)

use Jenssegers\Mongodb\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use HasFactory, Notifiable;

    protected $collection = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Post:

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Based on the Jessenger's readme doc, this is fairly standard and nothing special.

Post::all()->first() related values dump
  #attributes
    "_id" => MongoDB\BSON\ObjectId {#1601 ▶}
    "user" => "602f054f6294a33233745fab"

I saved the user using this, just getting the logged in user's ID.

$post->user = auth()->user()->getAuthIdentifier();

However, getting $post->user always returns null

$post = Post::all();
dd($post->user) // null

$post->user() works and returns the relationship, with the related value being the actual user.

Other posts suggested setting the foreign and local key

Post Class

public function user()
{
   return $this->belongsTo(User::class, 'user_id', '_id');
}

This doesn't work either.

Initially I saved the user as an ObjectId, but that doesn't work either.

My current idea is to just scrap the belongsTo functions entirely and just set the related IDs manually. The issue is that now I'll need to manually query for the user instead of the package doing so for me, or using Post::with('user') to autoload it.

How can I get the belongsTo relationship ID value?

  • PHP: 8.0.2
  • Laravel Framework: 8.28.1
  • Jessengers/MongoDB: 3.8.2
NeedHelp101
  • 599
  • 1
  • 9
  • 25

1 Answers1

1

I have found a bizarre work around, add user_id to the fillable field and fill that out

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'user_id'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

EG Controller

$post->user_id = $userID;

Then querying for the user works fine

$post = Post::all();
dd($post->user); // Gets the user, no longer null
dd($post->user_id); // This is the user id field you filled out before. Returns the user id

Why this works is beyond the realm of understanding for my puny mortal mind. Interestingly there's no need to set $post->user.

NeedHelp101
  • 599
  • 1
  • 9
  • 25