public function store($tags)
{
DB::transaction(function () use ($tags) {
$this->recursive($tags, null);
});
}
private function recursive($list, $parentId)
{
if ($this->checkToFillable($list)) {
$this->rules($list);
$id = DB::table('tags')->insertGetId(
array(
'name' => $list['name'],
'description' => $list['description'],
'parent_id' => $parentId
)
);
if ($parentId) {
$query = "INSERT INTO `tags_closure` (`ancestor_id`, `descendant_id`, `depth`)
SELECT `ancestor_id`, $id, `depth`+1
FROM `tags_closure`
WHERE `descendant_id` =" . $parentId;
DB::statement($query);
} else {
DB::table('tags_closure')->insert(
[
'ancestor_id' => $id,
'descendant_id' => $id,
'depth' => 0
]
);
}
if (isset($list['closureTag'])) {
$this->recursive($list['closureTag'], $id);
}
} else {
foreach ($list as $element) {
$this->recursive($element, $parentId);
}
}
}
private function rules($list)
{
try {
Validator::make([
'name' => $list['name'],
'description' => $list['description'],
], [
'name' => 'required|scripts',
'description' => 'required|scripts'
]);
} catch (ValidationException $e) {
return redirect('tags-closure')->withInput($e->getMessage());
}
}
private function rules($list)
$this->rules($list);
public function store($tags)
{
try {
DB::transaction(function () use ($tags) {
$this->recursive($tags, null);
});
} catch (ValidationException $e) {
return redirect('tags-closure')->withInput($e->getMessage());
}
}
private function recursive($list, $parentId)
{
if ($this->checkToFillable($list)) {
$this->validate($list);
$id = DB::table('tags')->insertGetId(
array(
'name' => $list['name'],
'description' => $list['description'],
'parent_id' => $parentId
)
);
if ($parentId) {
$query = "INSERT INTO `tags_closure` (`ancestor_id`, `descendant_id`, `depth`)
SELECT `ancestor_id`, $id, `depth`+1
FROM `tags_closure`
WHERE `descendant_id` =" . $parentId;
DB::statement($query);
} else {
DB::table('tags_closure')->insert(
[
'ancestor_id' => $id,
'descendant_id' => $id,
'depth' => 0
]
);
}
if (isset($list['closureTag'])) {
$this->recursive($list['closureTag'], $id);
}
} else {
foreach ($list as $element) {
$this->recursive($element, $parentId);
}
}
}
private function validate($list)
{
Validator::make([
'name' => $list['name'],
'description' => $list['description'],
], [
'name' => 'required|scripts',
'description' => 'required|scripts'
]);
}