Search code examples
laravelupgradelaravel-livewire

When upgrading to livewire v3, find error "No morph map defined for model [App\Models\{model}]."


I have a running Laravel app (Laravel 9.39.0 and PHP 8.1.3) over Docker, for which I am trying to upgrade Livewire from version 2.10 to 3.0 and I'm encountering an issue that I am not being able to solve.

To perform the upgrade, I follow the instructions from https://livewire.laravel.com/docs/upgrading, so I run:

sail composer require livewire/livewire "^3.0"

sail php artisan view:clear

sail php artisan livewire:upgrade

The last initiates the semi-automatic code review process, to which I consistently respond "keep" (1) and "yes". All the steps are completed without any issues. When finished, I proceed to name the parameters of dispatch(), thus completing the upgrade process.

I begin navigating through the app's pages, but as soon as I try to open any Laravel view containing a Livewire component, for example,

@livewire('authors.bios', ['author' => $author])

which was working fine with Livewire 2.10, I now encounter, in such line, the exception

"No morph map defined for model [App\Models\Author]."

I have searched everywhere for what could be causing this error but I can't find any clues, and I'm at a loss for what else to try.

Can anyone please help me?

PS. Here you can see my composer.json content:

{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "php": "^8.1",
    "doctrine/dbal": "^3.5",
    "ezyang/htmlpurifier": "^4.16",
    "fideloper/proxy": "~4.4.1",
    "guzzlehttp/guzzle": "^7.2",
    "laravel/framework": "^9.2",
    "laravel/sanctum": "^2.14.1",
    "laravel/tinker": "^2.7",
    "laravel/ui": "^3.4",
    "livewire/livewire": "^3.0"
},
"require-dev": {
    "fakerphp/faker": "^1.9.1",
    "laravel/sail": "^1.0.1",
    "mockery/mockery": "^1.4.4",
    "nunomaduro/collision": "^6.1",
    "phpunit/phpunit": "^9.5.10",
    "spatie/laravel-ignition": "^1.0"
},
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "app/helpers.php"
    ]
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
},
"scripts": {
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover --ansi"
    ],
    "post-update-cmd": [
        "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
    ],
    "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "@php artisan key:generate --ansi"
    ]
},
"extra": {
    "laravel": {
        "dont-discover": []
    }
},
"config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true

}


Solution

  • The error is usually shown when there are some polymorphic relations and an alias has been set for some of them using Relation::enforceMorphMap(), tipically in the AppServiceProvider::boot() method.
    Using enforceMorphMap() all the polymorphic relations must be listed also if they don't have an alias.
    To workaround this, you can use Relation::morphMap() instead of Relation::enforceMorphMap()

    You must check if the upgrade process has set enforceMorphMap()