2.1-2.4: install spatie/laravel-translatable, migrate to JSON, update models & controllers
Deploy Backend / deploy (push) Failing after 1m52s
Deploy Backend / deploy (push) Failing after 1m52s
This commit is contained in:
1 parent
75af8220a7
commit
37dd2c9d54
17 files changed
+453
-285
No files matched your search
@@ -11,13 +11,13 @@ class AboutMeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$aboutMe = AboutMe::with('skills.translations')->first();
|
||||
$aboutMe = AboutMe::with('skills')->first();
|
||||
return response()->json($aboutMe);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$aboutMe = AboutMe::with('skills.translations')->findOrFail($id);
|
||||
$aboutMe = AboutMe::with('skills')->findOrFail($id);
|
||||
return response()->json($aboutMe);
|
||||
}
|
||||
|
||||
@@ -42,24 +42,17 @@ class AboutMeController extends Controller
|
||||
|
||||
public function getTranslation($id, $locale)
|
||||
{
|
||||
$aboutMe = AboutMe::with(['translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
}, 'skills.translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
}])->findOrFail($id);
|
||||
$aboutMe = AboutMe::with('skills')->findOrFail($id);
|
||||
|
||||
if ($aboutMe->translations->isEmpty()) {
|
||||
if (!$aboutMe->hasTranslation('title', $locale)) {
|
||||
return response()->json(['message' => 'Translation not found', 'locale' => $locale], 404);
|
||||
}
|
||||
|
||||
$translation = $aboutMe->translations->first();
|
||||
$skills = $aboutMe->skills->map(function ($skill) use ($locale) {
|
||||
$translation = $skill->getTranslation($locale);
|
||||
|
||||
return [
|
||||
'id' => $skill->id,
|
||||
'title' => $translation ? $translation->title : null,
|
||||
'description' => $translation ? $translation->description : null,
|
||||
'title' => $skill->getTranslation('title', $locale),
|
||||
'description' => $skill->getTranslation('description', $locale),
|
||||
'image' => $skill->image,
|
||||
'link' => $skill->link,
|
||||
'percentage' => $skill->percentage,
|
||||
@@ -68,8 +61,8 @@ class AboutMeController extends Controller
|
||||
|
||||
$data = [
|
||||
'id' => $aboutMe->id,
|
||||
'title' => $translation->title,
|
||||
'description' => $translation->description,
|
||||
'title' => $aboutMe->getTranslation('title', $locale),
|
||||
'description' => $aboutMe->getTranslation('description', $locale),
|
||||
'image' => $aboutMe->image,
|
||||
'skills' => $skills,
|
||||
];
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -9,19 +9,17 @@ class ServiceController extends Controller
|
||||
{
|
||||
public function index($locale)
|
||||
{
|
||||
$services = Service::with(['translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
}])->get();
|
||||
$services = Service::all();
|
||||
|
||||
$response = $services->map(function ($service) use ($locale) {
|
||||
return [
|
||||
'id' => $service->id,
|
||||
'imageSrc' => $service->imageSrc,
|
||||
'altText' => $service->translations->first()->altText ?? '',
|
||||
'altText' => '',
|
||||
'created_at' => $service->created_at,
|
||||
'updated_at' => $service->updated_at,
|
||||
'title' => optional($service->translations->first())->title,
|
||||
'description' => optional($service->translations->first())->description,
|
||||
'title' => $service->getTranslation('title', $locale),
|
||||
'description' => $service->getTranslation('description', $locale),
|
||||
];
|
||||
});
|
||||
|
||||
@@ -30,13 +28,13 @@ class ServiceController extends Controller
|
||||
|
||||
public function showTranslation($id, $locale)
|
||||
{
|
||||
$service = Service::with('translations')->findOrFail($id);
|
||||
$service = Service::findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'id' => $service->id,
|
||||
'imageSrc' => $service->imageSrc,
|
||||
'title' => $service->getTitle($locale),
|
||||
'description' => $service->getDescription($locale),
|
||||
'title' => $service->getTranslation('title', $locale),
|
||||
'description' => $service->getTranslation('description', $locale),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user