# Walkthrough - Universal Dynamic API Engine

I have implemented a comprehensive, metadata-driven API engine that centralizes data fetching for all screens. This engine supports advanced filtering, full-text search, recursive relationship loading (up to 3 levels), and flexible pagination.

## Changes Made

### Backend (Laravel)

#### 1. Core Query Engine
I created a new service [EloquentDynamicQueryService.php](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Infrastructure/Services/EloquentDynamicQueryService.php) that handles:
- **Recursive Includes**: Automatically resolves nested relationships like `include=city,city.stations`.
- **Display Labels**: Automatically adds `{relationName}_label` (e.g., `city_label`) when a relation is included, resolving the actual name from the configured display column.
- **Translation Support**: Handles JSON-encoded labels in related data, correctly decoding them into objects or strings.
- **Dynamic Filtering**: Supports operators like `gt`, [lt](file:///d:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Infrastructure/Services/EloquentDynamicQueryService.php#75-104), `contains`, [in](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Presentation/Controllers/API/DynamicDataController.php#39-51) via the `filter[]` parameter.
- **Global Search**: Searches across all fields marked as `is_searchable`.
- **Field Selection**: Ability to fetch only specific columns via the [fields](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Infrastructure/Models/PageModel.php#18-22) parameter.

#### 2. Unified Application Layer
- **UnifiedDataFetchUseCase**: Orchestrates metadata retrieval and query execution.
- **Updated Controllers**: Both [DynamicDataController](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Presentation/Controllers/API/DynamicDataController.php) and [PublicPageController](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Presentation/Controllers/API/PublicPageController.php) now use this engine for their index/listing endpoints.

### Frontend (React)

#### 1. Metadata Management
- **Schema Update**: Updated [dynamic-page.schema.ts](file:///d:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/react-features-starter-kit/src/features/dynamic-pages/schemas/dynamic-page.schema.ts) to include `is_filterable`.
- **Field Configuration**: Added toggles for **Searchable** and **Filterable** in the [FieldDialog.tsx](file:///d:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/react-features-starter-kit/src/features/dynamic-pages/components/FieldDialog.tsx), allowing you to configure these properties per-field.

## How to Use the New API Engine

The engine is accessible at `/api/pages/{slug}` (Public) or `/api/dynamic-pages?route={slug}` (Admin). It supports:

| Parameter | Example | Description |
| :--- | :--- | :--- |
| `include` | `city,city.stations` | Load nested relations recursively. |
| `filter[]` | `filter[status]=active` | Filter by field values. |
| [search](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Presentation/Controllers/API/DynamicDataController.php#99-141) | `?search=makkah` | Search across searchable fields. |
| `sort_by` | `?sort_by=created_at` | Sort by a specific field. |
| [fields](file:///D:/NewTouch/Projects/Control%20Panel/NewTouch-Control-Panel/laravel-domain-kit/modules/DynamicPage/Infrastructure/Models/PageModel.php#18-22) | `?fields=id,name,lat,lng` | Select specific columns. |

## Important Next Steps

> [!IMPORTANT]
> Since the backend resides in a directory outside the currently active terminal workspace, you **MUST** run the migrations manualy to apply the new schema changes.

```powershell
# Navigate to the backend directory
cd "D:\NewTouch\Projects\Control Panel\NewTouch-Control-Panel\laravel-domain-kit"

# Run migrations
php artisan migrate
```

---

# Walkthrough - cPanel Image Storage Fix
(Previous walkthrough for image storage fix remains below)
... (Previous content summary)

