Search code examples
phplaravelupgradelaravel-5.3laravel-5.4

Laravel 5.4 from 5.3 : Error getOtherKey()


I was getting the relationship as in laravel 5.3 and was working fine:

//execute the relation of the given model
$data = $model->{$info["relation"]}();

// get the type of the relation
$class = get_class($data);
$dataType = explode("\\", $class);
$relationType = end($dataType);

$options["columns"][$key]["relationType"] = $relationType;

// if its a simple belongs-to statement
if($relationType == "BelongsTo") {

    // get all belongs-to query info
    $otherTable = $data->getRelated()->getTable();
    $foreignKey = $data->getQualifiedForeignKey();
    $otherKey = $data->getOtherKey();

    // manually join using it
    $retrievedRecords->leftJoin($otherTable . ' as ' . $info["relation"], $info["relation"] . '.' . $otherKey, '=', $foreignKey);

} else if($relationType == "HasMany" || $relationType == "HasOne") {

    // get all has-many query info
    $otherTable = $data->getRelated()->getTable();
    $foreignKey = $data->getPlainForeignKey();
    $parentKey = $data->getQualifiedParentKeyName();

    // manually join using it
    $retrievedRecords->leftJoin($otherTable . ' as ' . $info["relation"], $info["relation"] . '.' . $foreignKey, '=', $parentKey);

}

Now i downloaded fresh laravel 5.4 and it gives me error :

Call to undefined method Illuminate\Database\Query\Builder::getOtherKey()

As the getOtherKey() exists in the above code in if() section.

Is there any alternative for that ?


Solution

  • The getOtherKey method has been renamed to getOwnerKey. So you can get the owner key by saying:

    $ownerKey = $data->getOwnerKey();