Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('App\Opportunity', 'Organization');
        }
}

class Department extends model {

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

$org = App\Opportunity::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., 'App\Account' 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" => "App\Account"
    "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" => "App\Account"
    "Organization_id" => 456
    "ID" => 1
  ]
  #dateFormat: null
  #events: []
  #observables: []
  #relations: array:3 [
    "Organization" => null
    "Projects" => Illuminate\Database\Eloquent\Collection {#3463
      #items: []
    }
    "Tickets" => Illuminate\Database\Eloquent\Collection {#3443
      #items: []
    }
  ]
  #touches: []
  #forceDeleting: false
like image 315
Josh Bodine Avatar asked Mar 14 '17 23:03

Josh Bodine


1 Answers

change your morphto to this base on document to prevent confusing laravel to detect column type and column id

class Image extends Model
{
    protected $fillable = [
        "link",
        "imageable_id",
        "imageable_type",
    ];

    public function imagable()
    {
          return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
    }
}
like image 167
ghazaleh javaheri Avatar answered Oct 15 '22 06:10

ghazaleh javaheri