adrema/app/Group/Actions/GroupApiIndexAction.php

33 lines
864 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;
2024-08-25 14:10:22 +02:00
use Lorisleiva\Actions\ActionRequest;
2023-12-30 22:21:08 +01:00
use Lorisleiva\Actions\Concerns\AsAction;
class GroupApiIndexAction
{
use AsAction;
/**
* @return Collection<int, Group>
*/
public function handle(): Collection
{
return Group::get();
}
2024-08-25 14:10:22 +02:00
public function asController(ActionRequest $request, ?Group $group = null): AnonymousResourceCollection
2023-12-30 22:21:08 +01:00
{
2024-08-25 14:10:22 +02:00
return GroupResource::collection(
$request->has('all')
? Group::with('children')->get()
: ($group ? $group->children()->withCount('children')->get() : Group::where('parent_id', null)->withCount('children')->get())
);
2023-12-30 22:21:08 +01:00
}
}