Laravel self-reference Relationship and load() method
I have the following code:
public function othersFromSameLab(): HasMany
{
return $this->hasMany(self::class, 'laboratory_id', 'laboratory_id')
->whereNot('id', $this->id);
}
When I use the load method, e.g., $myModel->load('othersFromSameLab'), the whereNot constraint does not work because the id is null. However, accessing the relationship directly using $myModel->othersFromSameLab works as expected.
Is there a solution or a better approach to resolve this?
To use load, you should first get the model and then run load. like:
$user = User::all() or $user = User::where(some condition) and THEN use $user->load('relation') to load the relation.
On the contrary, using $user->where('some_condition')->with('relation') directly loads the relation.
Using load() gives you the ability to load only the main model, and decide later if the relation has to be loaded. Like, when using load(), you have 2 queries that are executed separately, while when using with() those 2 queries are executed simultaneously and the relation is accessible on the go.
$user = User::where(some condition) returns a query builder
->load() must only be used on a hydrated instance of the model in order for the id to be set. The OP does not explain what $myModel contains so we can't say if the example should or should not work.
@ismail_bourbie the best solution is to ensure that laboratory_id is null on its own record, and then you don't need the additional where condition which breaks things like ->with()
public function laboratory(): BelongsTo
{
return $this->belongsTo(Laboratory::class);
}
public function othersFromSameLab(): HasMany
{
return $this->hasMany(self::class, 'laboratory_id', 'laboratory_id')
->whereNot('id', $this->id);
}
public function laboratory(): BelongsTo
{
return $this->belongsTo(Laboratory::class);
}
public function othersFromSameLab(): HasMany
{
return $this->hasMany(self::class, 'laboratory_id', 'laboratory_id')
->whereNot('id', $this->id);
}
@ismail_bourbie this relationship definition must be incorrect; the local key and foreign should be different for a self referencing relationship. Assuming the local key is id, then: