Answer the question
In order to leave comments, you need to log in
Database optimization. How to make a relationship query and field formation in one query?
Greetings colleagues, the essence is this, there is a model with relationships that refers to itself
public function getUnitedNameAttribute()
{
$additional_name = $this->dependantGroup()->get();
$name = $this->name;
return $name . (($additional_name->count() > 0) ? " / " .$additional_name->first()->name : "");
}
public function dependantGroup()
{
return $this->hasOne(Group::class, 'united_with');
}
public function parentGroup()
{
return $this->belongsTo(Group::class,'id');
}
Answer the question
In order to leave comments, you need to log in
You need to use the with() method if I understood you correctly
$groups = Group::with('dependantGroup', 'parentGroup')->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question