-
app/Http/Resources/Api/TagResource.php
Open in GitHubuse Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class TagResource extends JsonResource { public function toArray(Request $request): array { return [ 'id' => $this->id, 'workspace_id' => $this->workspace_id, 'name' => $this->name, 'color' => $this->color, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]; } }
-
app/Http/Controllers/Api/TagController.php
Open in GitHubuse Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; use App\Http\Requests\Tag\CreateRequest; use App\Models\Tag; class TagController extends Controller { public function index(Request $request) { $tags = Tag::where('workspace_id', $request->workspace->id) ->latest() ->paginate(config('app.pagination.default')); return TagResource::collection($tags); } public function store(CreateRequest $request) { $response = Gate::inspect('reached-tag-limit', $request->workspace); if (!$response->allowed()) { return response()->json(['message' => 'You have reached the tag limit'], 403); } $tag = Tag::create([ 'workspace_id' => $request->workspace->id, 'name' => $request->name, 'color' => $request->color, 'sort' => Tag::where('workspace_id', $request->workspace->id)->count() + 1, ]); return response()->json(new TagResource($tag), 201); } // ... }