Improving laravel relationship performance
This commit is contained in:
@@ -38,14 +38,21 @@ class Has_One extends Has_One_Or_Many {
|
||||
{
|
||||
$foreign = $this->foreign_key();
|
||||
|
||||
foreach ($parents as &$parent)
|
||||
$children_hash = array();
|
||||
foreach ($children as $child)
|
||||
{
|
||||
$matching = array_first($children, function($k, $v) use (&$parent, $foreign)
|
||||
if (array_key_exists($child->pivot->$foreign, $children_hash))
|
||||
{
|
||||
return $v->$foreign == $parent->get_key();
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
$parent->relationships[$relationship] = $matching;
|
||||
$children_hash[$child->pivot->$foreign] = $child;
|
||||
}
|
||||
|
||||
foreach ($parents as $parent)
|
||||
{
|
||||
if (array_key_exists($parent->get_key(), $children_hash))
|
||||
$parent->relationships[$relationship] = $children_hash[$parent->get_key()];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user