query('per_page', 10); // مقدار پیش‌فرض ۱۰ در نظر گرفته می‌شود // Retrieve projects with translations and categories $projects = Project::with('translations', 'category.translations')->paginate($perPage); $formattedProjects = $projects->items(); $formattedProjects = array_map(function ($project) use ($locale) { $translation = $project->translations->firstWhere('locale', $locale); $categoryTranslation = $project->category->translations->firstWhere('locale', $locale); return [ 'id' => $project->id, 'title' => $translation ? $translation->title : null, 'category' => $categoryTranslation ? $categoryTranslation->title : null, 'images' => [ ['src' => $project->image1, 'position' => '1', 'zIndex' => 1], ['src' => $project->image2, 'position' => '2', 'zIndex' => 2], ], 'created_at' => $project->created_at, 'updated_at' => $project->updated_at, ]; }, $formattedProjects); return response()->json([ 'projects' => [ 'current_page' => $projects->currentPage(), 'data' => $formattedProjects, 'total' => $projects->total(), 'last_page' => $projects->lastPage(), ], 'categories' => ProjectCategory::all()->map(function ($category) use ($locale) { return [ 'id' => $category->id, 'title' => $category->translations->firstWhere('locale', $locale)->title ?? null, 'image' => $category->image, ]; }), ]); } public function store(Request $request) { $project = Project::create($request->only(['category_id', 'image1', 'image2'])); // ایجاد ترجمه‌ها foreach (['fa', 'en'] as $locale) { $project->translations()->create([ 'locale' => $locale, 'title' => $request->input("title_$locale"), 'description' => $request->input("description_$locale"), ]); } return response()->json($project->load('translations'), 201); } public function show($id) { $project = Project::with('translations', 'category.translations')->findOrFail($id); $translation = $project->translations->firstWhere('locale', 'fa'); $categoryTranslation = $project->category->translations->firstWhere('locale', 'fa'); $formattedProject = [ 'id' => $project->id, 'title' => $translation ? $translation->title : null, 'category' => $categoryTranslation ? $categoryTranslation->title : null, 'images' => [ ['src' => $project->image1, 'position' => '1', 'zIndex' => 1], ['src' => $project->image2, 'position' => '2', 'zIndex' => 2], ], 'created_at' => $project->created_at, 'updated_at' => $project->updated_at, ]; return response()->json(['project' => $formattedProject]); } public function update(Request $request, $id) { $project = Project::findOrFail($id); $project->update($request->only(['category_id', 'image1', 'image2'])); // به‌روزرسانی ترجمه‌ها foreach (['fa', 'en'] as $locale) { $project->translations()->updateOrCreate( ['locale' => $locale], [ 'title' => $request->input("title_$locale"), 'description' => $request->input("description_$locale"), ] ); } return response()->json($project->load('translations')); } public function destroy($id) { Project::destroy($id); return response()->json(null, 204); } }