将父值放在同一个数组(Concate)中的最佳方法是什么,因为此代码只返回一个值
public function divisionParent($name)
{
$path = array();
$path[] = $name;
$div = CsiCategory::where('name', $name)->first();
$parent_id = $div->parent_id;
if ($parent_id != 0) {
$name = CsiCategory::where('id', $parent_id)->first();
$this->divisionParent($name->name);
}
return $path;
}
答案 0 :(得分:0)
这样的事可能吗?
public function divisionParent($name, $path = [])
{
// Append to $path arr.
array_push($path, $name);
// Get division by name.
$div = CsiCategory::where('name', $name)->first();
// Check for existing parent division by id.
if (isset($div)) {
if ($div->parent_id != 0) {
$divParent = CsiCategory::where('id', $div->parent_id)->first();
}
}
return isset($divParent) ? $this->divisionParent($divParent->name, $path) : $path;
}