*/ private const RESOURCES = [ 'posts' => Post::class, 'categories' => Category::class, 'tags' => Tag::class, 'projects' => Project::class, 'project-categories' => ProjectCategory::class, 'about-me' => AboutMe::class, 'comments' => Comment::class, ]; /** * Relations eager loaded per resource, so an editing form is populated * from a single request instead of a fan-out of lookups. * * @var array> */ private const RELATIONS = [ 'posts' => ['tags'], 'projects' => ['category'], 'skills' => ['aboutMe'], 'testimonials' => ['translations'], 'comments' => ['post'], ]; /** * Raw rows for content management. * * The public API intentionally returns display-oriented payloads: single * translated values, reshaped image arrays, no internal ids for some * fields. An editing form needs the stored row instead, including both * locales, so this endpoint serialises the model as-is. */ public function index(Request $request, string $resource): JsonResponse { $resources = array_merge(self::RESOURCES, AdminCrudController::resources()); $model = $resources[$resource] ?? null; if ($model === null) { return response()->json(['message' => 'Unknown admin resource.'], 404); } $rows = $model::query()->with(self::RELATIONS[$resource] ?? [])->orderByDesc('id')->get(); return response()->json(['data' => $rows]); } /** * Field names accepted per resource, so the admin panel and the write * endpoints cannot drift apart silently. */ public static function writableFields(string $resource): array { return match ($resource) { 'posts' => ['title', 'body', 'excerpt', 'slug', 'author_id', 'category_id', 'featured_image', 'views', 'likes', 'published_at', 'tags'], 'categories' => ['name', 'slug', 'description', 'short_description', 'image'], 'tags' => ['name', 'slug'], 'projects' => ['category_id', 'image1', 'image2', 'title_fa', 'title_en', 'description_fa', 'description_en'], 'about-me' => ['image', 'title_fa', 'title_en', 'description_fa', 'description_en'], 'services' => ['imageSrc', 'translations'], 'skills' => ['image', 'link', 'percentage', 'about_me_id', 'translations'], 'testimonials' => ['image', 'client_link', 'translations'], default => [], }; } }