2.1-2.4: install spatie/laravel-translatable, migrate to JSON, update models & controllers

This commit is contained in:
parsa aghaei committed 2026-06-23 18:31:51 +03:30
1 parent 1dc72d8563
commit db7ce880a0
17 files changed
+453 -285

No files matched your search

@@ -14,17 +14,14 @@ class ProjectController extends Controller
{
$perPage = request()->query('per_page', 10);
$projects = Project::with('translations', 'category.translations')->paginate($perPage);
$projects = Project::with('category')->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,
'title' => $project->getTranslation('title', $locale),
'category' => $project->category?->getTranslation('title', $locale),
'images' => [
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
@@ -44,7 +41,7 @@ class ProjectController extends Controller
'categories' => ProjectCategory::all()->map(function ($category) use ($locale) {
return [
'id' => $category->id,
'title' => $category->translations->firstWhere('locale', $locale)->title ?? null,
'title' => $category->getTranslation('title', $locale),
'image' => $category->image,
];
}),
@@ -56,27 +53,22 @@ class ProjectController extends Controller
$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"),
]);
$project->setTranslation('title', $locale, $request->input("title_$locale"));
$project->setTranslation('description', $locale, $request->input("description_$locale"));
}
$project->save();
return response()->json($project->load('translations'), 201);
return response()->json($project, 201);
}
public function show($id)
public function show($id, $locale)
{
$project = Project::with('translations', 'category.translations')->findOrFail($id);
$translation = $project->translations->firstWhere('locale', 'fa');
$categoryTranslation = $project->category->translations->firstWhere('locale', 'fa');
$project = Project::with('category')->findOrFail($id);
$formattedProject = [
'id' => $project->id,
'title' => $translation ? $translation->title : null,
'category' => $categoryTranslation ? $categoryTranslation->title : null,
'title' => $project->getTranslation('title', $locale),
'category' => $project->category?->getTranslation('title', 'fa'),
'images' => [
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
@@ -94,16 +86,16 @@ class ProjectController extends Controller
$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"),
]
);
if ($request->has("title_$locale")) {
$project->setTranslation('title', $locale, $request->input("title_$locale"));
}
if ($request->has("description_$locale")) {
$project->setTranslation('description', $locale, $request->input("description_$locale"));
}
}
$project->save();
return response()->json($project->load('translations'));
return response()->json($project);
}
public function destroy($id)