JohnnyL
419
3
Laravel

Attach NULL

Posted 2 years ago by JohnnyL

I need to make a relation for every type in my database, even if it doesnt exists.

It's a self referencing pivot on the user table and this attach query works fine:

$user->supervisor()->attach([$supervisor => ['type' => $type]]);

However, when the supervisor is not a user but 'External' I run into problems because you can't seem to attach NULL. What would be a good way to handle this?

   public function supervisors()
        {
        return $this->belongsToMany(User::class, 'supervisors', 'user', 'supervisor');
        }

Please sign in or create an account to participate in this conversation.