Skip to content
Advertisement

Laravel morphTo Returns Null

I am using Laravel 5.4 and having trouble figuring out why my morphTo relationship always returns null no matter what I do. The inverse of the relationship is fine, but when I try to retrieve the owner of the polymorphic relation, it is null.

class Opportunity extends Model {

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

}

class Account extends model {

    public function Opportunities() {
            return $this->morphMany('AppOpportunity', 'Organization');
        }
}

class Department extends model {

    public function Opportunities() {
            return $this->morphMany('AppOpportunity', 'Organization');
        }
}

$org = AppOpportunity::findOrFail(1)->Organization;

The full namespace is stored in the database and the _type and _id actually have the appropriate organization type and id in the columns (i.e., ‘AppAccount’ and ‘456’). So, I know the database record and the returned Opportunity object have the correct Organization in the columns (I can see it in the database correctly), but no matter what I do, if I try to retrieve Organization it is null.

Here is the output. You will notice the Organization is in the attributes, but the relation is null and I cannot get it to return even adding ->with(‘Organization’) to the query. It doesn’t even seem to be executing the query to get the owner

#primaryKey: "ID"
  +timestamps: true
  #guarded: []
  #hidden: []
  #visible: []
  #with: []
  #dissociateRelations: []
  #connection: null
  #keyType: "int"
  +incrementing: true
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: true
  #attributes: array:13 [
    "StageID" => 12
    "TypeID" => 1
    "OriginID" => 20
    "Description" => "Interested in scanner fi6140"
    "UserID" => 3
    "SolutionValue" => ".00"
    "MarginValue" => ".00"
    "created_at" => "2010-09-16 11:19:00.000"
    "updated_at" => "2015-09-01 12:32:00.000"
    "_migrationID" => "4299"
    "Organization_type" => "AppAccount"
    "Organization_id" => 456
    "ID" => 1
  ]
  #original: array:13 [
    "StageID" => 12
    "TypeID" => 1
    "OriginID" => 20
    "Description" => "Interested in scanner fi6140"
    "UserID" => 3
    "SolutionValue" => ".00"
    "MarginValue" => ".00"
    "created_at" => "2010-09-16 11:19:00.000"
    "updated_at" => "2015-09-01 12:32:00.000"
    "_migrationID" => "4299"
    "Organization_type" => "AppAccount"
    "Organization_id" => 456
    "ID" => 1
  ]
  #dateFormat: null
  #events: []
  #observables: []
  #relations: array:3 [
    "Organization" => null
    "Projects" => IlluminateDatabaseEloquentCollection {#3463
      #items: []
    }
    "Tickets" => IlluminateDatabaseEloquentCollection {#3443
      #items: []
    }
  ]
  #touches: []
  #forceDeleting: false

Advertisement

Answer

So, it looks like I may have discovered my problem, but I do not know why. When the owner is queried by

AppOpportunity::findOrFail(1)->Organization

it looks like Eloquent is looking for organization_type and organization_id (with lowercase) and not Organization_type and _id. However, my migration uses $table->morphs(‘Organization’) and so the columns in the database are created with the uppercase. When I change that to lowercase in the database, my results get returned. Not sure how to change that behavior though, and it seems to have been introduced after upgrading from 5.2

Edit: there was a change introduced in 5.3 that snake cases the _type and _id that seems to be the root cause of my experience

https://github.com/laravel/framework/pull/15334

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement