Skip to content

Morph relationships does't support camel case database field name like 'userType' #25440

@jason-chang

Description

@jason-chang
  • Laravel Version: 5.5.43
  • PHP Version: 7.2.7
  • Database Driver & Version: Mysql 5.7

Description:

Morph relationships �does't support camel case database field name;

Steps To Reproduce:

this problem is because line in MorphToMany class

    public function __construct(Builder $query, Model $parent, $name, $table, $foreignPivotKey,
                                $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false)
    {
        $this->inverse = $inverse;
        $this->morphType = $name.'_type';   // Notice: problem here 
        $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass();

        parent::__construct(
            $query, $parent, $table, $foreignPivotKey,
            $relatedPivotKey, $parentKey, $relatedKey, $relationName
        );
    }

And line in HasRelationships trait

    protected function getMorphs($name, $type, $id)
    {
        return [$type ?: $name.'_type', $id ?: $name.'_id'];  // Notice: problem here 
    }

I think if we can add a configration in global database config file, like 'field_name_case'; and values can be snake or camel ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions