public function morphTo($name = null, $type = null, $id = null)
{
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if (is_null($name)) {
list(, $caller, $backtrace) = debug_backtrace(false);
// Use custom relationship bindings
if ($backtrace['function'] == 'callRelationship') {
$relation = $backtrace['args'][0];
} else {
$relation = $caller['function'];
}
$name = snake_case($relation);
}
list($type, $id) = $this->getMorphs($name, $type, $id);
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->{$type})) {
return new MorphTo($this->newQuery(), $this, $id, null, $type, $name);
} else {
if (method_exists($this, 'getActualClassNameForMorph')) {
$class = $this->getActualClassNameForMorph($class);
}
$instance = new $class();
return new MorphTo(with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name);
}
}