Skip to content

Unable to use relationships to Model named Attribute after upgrading to 11.x #58623

@jsgibsoncrea

Description

@jsgibsoncrea

Laravel Version

11.48.0

PHP Version

8.3.0

Database Driver & Version

No response

Description

I'm working on upgrading versions from 9.x to 12.x and am going step-by-step. We are currently in 11.x as indicated in the form.

Our application makes use of a table named attributes it's related to another table named project_attributes which is a pivot mapping multiple projects to multiple attributes. After moving from 10.x -> 11.x, we are no longer able to lazy load the attribute relationship from our ProjectAttribute model, nor are we able to reference the attribute_id property on a ProjectAttribute.

Each yields its respective error like so: Undefined property: App\\Models\\ProjectAttribute::$attribute_id

This application is a decade old and I can't change table names or column names of the affected entities. I'm happy to implement a work-around if someone has one; for example, I tried replacing all the instances of lazy loading attribute with attribute->first()? and this solved the issue with that. I'm unable to manually fix the reference to attribute_id as this is called within the BelongsTo code.

Steps To Reproduce

Have a table attributes and model named Attribute, create a second model related to this table that relates to it via BelongsTo.

in my case this is:

<?php

class Attribute extends Model {
}

class ProjectAttribute extends Model {
   public function attribute(): BelongsTo
   {
       return $this->belongsTo(Attribute::class);
   }
}

$pa = ProjectAttribute::first();

$pa->attribute;  //Undefined property: App\\Models\\ProjectAttribute::$attribute
$pa->attribute_id; //Undefined property: App\\Models\\ProjectAttribute::$attribute_id

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