jeudi 16 mai 2019

In Laravel 5.8, How do I setup a polymorphic relationship between a Model and a package Model?

I have a package model named Company\Contact\Models\Contact

I have a local model named App\Models\MenuItem

Menu item is a polymorphic table with:

id
menuable_id  
menuable_type

When I use it on local models like \App\Models\Page it works fine. When I call the menuable relationship in my MenuItem model, this one:

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

$menuitem->menuable it gives me an instance of the page model, great, perfect. But when I do this on any package model it is null.

I tried binding my model:

$this->app->bind('Company\Contact\Models\Contact', function () {
    return new Contact;
});

But that didn't work.

I then tried mapping it in the AppServiceProvider boot:

use Company\Contact\Models\Contact; // This does dump an instance of the contact model. 

Relation::morphMap([
    'Company\Contact\Models\Contact' => Contact::class,
]);

Anybody know how to do this?

Please let me know if I can provide any more information.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire