Función recursiva de PHP Laravel con matriz anidada

Estoy usando Laravel 4 con MySQL back-end.

Quiero almacenar registros de una nested array en la base de datos utilizando la recursive function .

La matriz de entrada es la siguiente:

 Array ( [0] => Array ( 'id' => 561, 'type' => 'q', 'subtype' => 'boolean', 'title' => 'Did you..?', 'parent' => 560, 'created_at' => '2014-07-09 09:45:50', 'updated_at' => NULL, 'deleted_at' => NULL, 'children' => Array ( [0] => Array ( 'id' => 562, 'type' => 'o', 'subtype' => NULL, 'title' => 'Yes', 'parent' => 561, 'created_at' => '2014-07-09 09:45:50', 'updated_at' => NULL, 'deleted_at' => NULL, 'children' => Array ( ) ) [1] => Array ( 'id' => 563, 'type' => 'answer', 'subtype' => NULL, 'title' => 'No', 'parent' => 561, 'created_at' => '2014-07-09 09:45:50', 'updated_at' => 'NULL', 'deleted_at' => NULL, 'children' => Array ( ) ) ) ) ) 

La recursive function que uso para almacenar los registros en la base de datos es la siguiente:

 public function addRecursiveChildren(array $surveychildren, $parentid, $userid){ foreach ($surveychildren as $item) { /* Error : HTTPRequest Error :: 500: {"error":{"type":"ErrorException","message":"Cannot use a scalar value as an array Error is in the statement below in the second recursion when child is passes as an input. */ $item['survey_id'] = $item['id']; $item['user_id'] = $userid; $item['id'] = null; $item['parent'] = $parentid; $routine = routine::create($item); if(count($item["children"]) > 0) { foreach ($item["children"] as $child) { /* The $child I found is as below : $child = array( 'id' => 562, 'type' => 'o', 'subtype' => NULL , 'title' => 'Yes', 'parent' => 561, 'created_at' => '2014-07-09 09:45:50', 'updated_at' => NULL, 'deleted_at' => NULL, 'children' => Array ( ) ); */ RoutineRepository::addRecursiveChildren($child, $routine->id, $userid); } } } } 

Editar:

Sé que la causa del error es el $child que estoy pasando como una input array a la recursive function anterior:

El $child es algo como esto:

 array( 'id' => 562, 'type' => 'o', 'subtype' => NULL , 'title' => 'Yes', 'parent' => 561, 'created_at' => '2014-07-09 09:45:50', 'updated_at' => NULL, 'deleted_at' => NULL, 'children' => Array ( ) ) 

En lugar de esto, si $child será algo como esto:

 Array ( [0] => array( 'id' => 562, 'type' => 'o', 'subtype' => NULL , 'title' => 'Yes', 'parent' => 561, 'created_at' => '2014-07-09 09:45:50', 'updated_at' => NULL, 'deleted_at' => NULL, 'children' => Array ( ) ) ) 

Entonces no habrá error.

¿Alguien puede ayudarme a superarlo?

Gracias.

Esto debería funcionar

 class Routine extends \Eloquent { // The relation public function subRoutine() { return $this->hasMany('Routine', 'parent'); } public function saveSubroutine(array $children) { foreach($children as $childData) { $child = new self($childData); $this->subRoutine()->save($child); $child->saveSubroutine($childData['children']); } } } $routine = Routine::create($data); $routine->saveSubroutine($data['children']);