adrema/app/Group/Actions/GroupApiIndexAction.php

30 lines
733 B
PHP
Raw Normal View History

2023-12-30 22:21:08 +01:00
<?php
namespace App\Group\Actions;
use App\Group;
use App\Group\Resources\GroupResource;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Inertia\Inertia;
use Inertia\Response;
use Lorisleiva\Actions\Concerns\AsAction;
class GroupApiIndexAction
{
use AsAction;
/**
* @return Collection<int, Group>
*/
public function handle(): Collection
{
return Group::get();
}
public function asController(?Group $group = null): AnonymousResourceCollection
{
return GroupResource::collection($group ? $group->children()->withCount('children')->get() : Group::where('parent_id', null)->withCount('children')->get());
}
}