Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

morphMap Issue #1

Closed
sineld opened this issue Oct 3, 2019 · 5 comments
Closed

morphMap Issue #1

sineld opened this issue Oct 3, 2019 · 5 comments
Assignees

Comments

@sineld
Copy link

sineld commented Oct 3, 2019

Using,

        Relation::morphMap([
            'App\Models\Profile',
            'App\Models\User',
        ]);

in AppServiceProvider breaks application. I'm not sure if it is related to your package.
Please check.

@michaelbaril
Copy link
Owner

Hi,
I would need more information to understand the context. Could you provide me with the error message and stack trace?
Thanks

@sineld
Copy link
Author

sineld commented Oct 3, 2019

Let me explain, when using Polymorhic relations, Laravel writes the whole Model with the namespace (App\User), but when we add the morphMap to AppServiceProvider like below:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    public function boot()
    {
        Relation::morphMap([
            'App\User',
        ]);
    }
}

App\User becomes users and this occurs error below:

/Users/sineld/Code/laravel58/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
    public function getMorphClass()
    {
        $morphMap = Relation::morphMap();
 
        if (! empty($morphMap) && in_array(static::class, $morphMap)) {
            return array_search(static::class, $morphMap, true);
        }
 
        return static::class;
    }
 
    /**
     * Create a new model instance for a related model.
     *
     * @param  string  $class
     * @return mixed
     */
    protected function newRelatedInstance($class)
    {
        return tap(new $class, function ($instance) {
            if (! $instance->getConnectionName()) {
                $instance->setConnection($this->connection);
            }
        });
    }
 
    /**
     * Get all the loaded relations for the instance.
     *
     * @return array
     */
    public function getRelations()
    {
        return $this->relations;
    }
 
    /**
     * Get a specified relationship.
     *
     * @param  string  $relation
Arguments
"Class 'users' not found"

Please see images attached.
2019-10-03 22-27-28 (MySQL 5 7 27) localhost homestead searchindex (2)
2019-10-03 22-37-03 Whoops! There was an error

@michaelbaril michaelbaril self-assigned this Oct 3, 2019
@michaelbaril
Copy link
Owner

michaelbaril commented Oct 3, 2019

Indeed, it's a bug. It should be fixed with the latest commit on master, please check.
By the way, you should do a full reindex after you've changed the morph map.

@sineld
Copy link
Author

sineld commented Oct 3, 2019

Yes, it's solved. Thanks. You may close the issue.

@michaelbaril
Copy link
Owner

Thanks for your feedback. I've included the fix in the latest release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants