Home > Software engineering >  What’s the purpose of the $relation parameter in HasRelationships->belongsTo()?
What’s the purpose of the $relation parameter in HasRelationships->belongsTo()?

Time:08-12

So I was wandering in the source code of the HasRelationsShips trait and found that there’s this $relation parameter in methods like belongsTo that doesn’t show up in Laravel’s doc examples and by the looks of it serves only to debug purposes. Is this correct?

CodePudding user response:

It is not for debug purposes, it is so you can define the relation name explicitly. Without that argument the BelongsTo will implicitly use the calling method name as the 'relation' name, example:

function user() { 
    return $this->belongsTo(User::class);
}

This will use a backtrace to know that the belongsTo was called from a method named user. So it will know the relation is user. Since we didn't explicitly tell it any foreign key it will use the relation, user, to build the foreign key: $relation .'_'. $relatedInstance->getKeyName() which would build user_id. This relation name is also used so that other relationship functionality knows what the name of the relationship method is on this model for this relationship, such as the associate and disassociate methods.

CodePudding user response:

Just to elaborate on lagbox's answer, the use of a backtrace happens in Illuminate\Database\Eloquent\Concerns\HasRelations::guessBelongsToRelation() and looks like this:

protected function guessBelongsToRelation()
{
    [$one, $two, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
    return $caller['function'];
}

The use of debug_backtrace() is controversial – and I'm not a big fan of micro-optimizations generally – but in this case with modern PHP it's very easy to skip the function altogether.

All my belongs-to relationships are simply defined like this, using a named argument and magic constant:

public function user(): BelongsTo
{
    return $this->belongsTo(User::class, relation: __FUNCTION__);
}
  • Related