adrema/app/Initialize/InitializeGroups.php

28 lines
711 B
PHP
Raw Normal View History

2022-03-11 20:19:17 +01:00
<?php
namespace App\Initialize;
use App\Group;
use Zoomyboy\LaravelNami\Api;
2022-03-11 20:19:17 +01:00
class InitializeGroups
{
private Api $api;
2022-03-11 20:19:17 +01:00
public function __construct(Api $api)
{
$this->api = $api;
}
public function handle(): void
{
2022-03-11 20:19:17 +01:00
$this->api->groups()->each(function ($group) {
$parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]);
$this->api->subgroupsOf($group->id)->each(function ($subgroup) use ($parent) {
Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]);
});
});
}
}