Laravel one to one (polymorphic) not working only on specific model

Viewed 204

I am facing a super weird issue.

I have 3 tables and their equivalent model:

table estructure

App\User

public function company() {
        return $this->morphTo();
    }

App\Supplier

public function user() {
    return $this->morphOne(User::class, 'company');
}

App\Company

public function users() {
        return $this->morphMany(User::class, 'company');
    }

For some reason the relationship on App\Supplier does not work, but all the other works normal, I can even get the supplier if I have the user:

$supplier = \App\Supplier::find(1);
$company = \App\Company::find(2);
$supplieruser = \App\User::find(1);

$supplier->user //THIS RETURN NULL
$company->users //return collection of users normally
$suppplieruser->company //returns an instance of supplier model

I have tried, changing the name of the relationship and nothing.

Any idea?

1 Answers

In case someone find this in the future... the relationship was not working because I had in my AppServiceProvider the following:

Relation::morphMap([
.....
            'supplier_to_supplier' => 'App\Supplier',
.....
        ]);

It seems that registering it here will affect all the morphs from that model.

Related