Can't get a belongsTo working when assigning to user
Created 6 years ago by edster

My attachment

class AttachRealtorRelationToUser {

    public function handle()
    {
        $user = new UserModel();

        /* @var Hookable $user */
        if (!method_exists($user, 'bind')) {
            return;
        }

        /**
         * Get the user's buildings.
         *
         * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
         */
        $user->bind(
            'realtor',
            function () {
                /* @var UserModel $this */
                return $this->belongsTo(RealtorModel::class);
            }
        );
    }
}

I can query from realtor -> user via assigned field on the realtor stream, its getting $user->realtor that isn't working

If I dd $user->realtor I get the relation returned, but $user->realtor just always returns null. I tried forcing the column relations in the belongsTo but that isn't working eaither =/

edster  —  6 years ago Best Answer

Had it backwards, was supposed to be a hasOne and had to use ->first() when querying