2.1-2.4: install spatie/laravel-translatable, migrate to JSON, update models & controllers
Deploy Backend / deploy (push) Failing after 1m52s

This commit is contained in:
parsa aghaei committed 2026-06-23 18:31:51 +03:30
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),
]);
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
+3 -37
View File
@@ -4,50 +4,16 @@ namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class AboutMe extends Model
{
use HasFactory;
use HasFactory, HasTranslations;
protected $table = 'website_about_me';
protected $fillable = ['image'];
/**
* ارتباط با ترجمه‌ها
*/
public function translations()
{
return $this->hasMany(AboutMeTranslation::class, 'about_me_id');
}
/**
* گرفتن ترجمه بر اساس زبان فعلی
*/
public function getTranslation($locale)
{
if ($this->relationLoaded('translations')) {
return $this->translations->firstWhere('locale', $locale);
}
return $this->translations()->where('locale', $locale)->first();
}
/**
* گرفتن عنوان بر اساس زبان فعلی
*/
public function getTitle($locale)
{
$translation = $this->getTranslation($locale);
return $translation ? $translation->title : null;
}
/**
* گرفتن توضیحات بر اساس زبان فعلی
*/
public function getDescription($locale)
{
$translation = $this->getTranslation($locale);
return $translation ? $translation->description : null;
}
protected array $translatable = ['title', 'description'];
public function skills()
{
-22
View File
@@ -1,22 +0,0 @@
<?php
namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AboutMeTranslation extends Model
{
use HasFactory;
protected $table = 'website_about_me_translations';
protected $fillable = ['about_me_id', 'locale', 'title', 'description'];
/**
* ارتباط با AboutMe
*/
public function aboutMe()
{
return $this->belongsTo(AboutMe::class, 'about_me_id');
}
}
+4 -6
View File
@@ -4,18 +4,16 @@ namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class Project extends Model
{
use HasFactory;
use HasFactory, HasTranslations;
protected $table = 'website_projects'; // نام صحیح جدول
protected $table = 'website_projects';
protected $fillable = ['category_id', 'image1', 'image2'];
public function translations()
{
return $this->hasMany(ProjectTranslation::class, 'project_id');
}
protected array $translatable = ['title', 'description'];
public function category()
{
+4 -6
View File
@@ -4,16 +4,14 @@ namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class ProjectCategory extends Model
{
use HasFactory;
use HasFactory, HasTranslations;
protected $table = 'website_project_categories'; // نام صحیح جدول
protected $table = 'website_project_categories';
protected $fillable = ['image'];
public function translations()
{
return $this->hasMany(ProjectCategoryTranslation::class, 'category_id');
}
protected array $translatable = ['title', 'description'];
}
@@ -1,14 +0,0 @@
<?php
namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProjectCategoryTranslation extends Model
{
use HasFactory;
protected $table = 'website_project_category_translations'; // نام صحیح جدول
protected $fillable = ['category_id', 'locale', 'title', 'description'];
}
-19
View File
@@ -1,19 +0,0 @@
<?php
namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProjectTranslation extends Model
{
use HasFactory;
protected $table = 'website_project_translations'; // نام صحیح جدول
protected $fillable = ['project_id', 'locale', 'title', 'description'];
public function project()
{
return $this->belongsTo(Project::class, 'project_id');
}
}
+5 -39
View File
@@ -4,48 +4,14 @@ namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class Service extends Model
{
use HasFactory;
use HasFactory, HasTranslations;
protected $table = 'website_services'; // نام جدول
protected $fillable = ['imageSrc']; // فیلدهایی که قابل پر کردن هستند
protected $table = 'website_services';
protected $fillable = ['imageSrc'];
/**
* ارتباط با جدول ترجمه‌ها
*/
public function translations()
{
return $this->hasMany(ServiceTranslation::class, 'service_id');
}
/**
* گرفتن ترجمه بر اساس زبان فعلی
*/
public function getTranslation($locale)
{
if ($this->relationLoaded('translations')) {
return $this->translations->firstWhere('locale', $locale);
}
return $this->translations()->where('locale', $locale)->first();
}
/**
* گرفتن عنوان بر اساس زبان فعلی
*/
public function getTitle($locale)
{
$translation = $this->getTranslation($locale);
return $translation ? $translation->title : null;
}
/**
* گرفتن توضیحات بر اساس زبان فعلی
*/
public function getDescription($locale)
{
$translation = $this->getTranslation($locale);
return $translation ? $translation->description : null;
}
protected array $translatable = ['title', 'description'];
}
-22
View File
@@ -1,22 +0,0 @@
<?php
namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ServiceTranslation extends Model
{
use HasFactory;
protected $table = 'website_service_translations'; // نام جدول
protected $fillable = ['service_id', 'locale', 'title', 'description']; // فیلدهایی که قابل پر کردن هستند
/**
* ارتباط با جدول خدمات
*/
public function service()
{
return $this->belongsTo(Service::class, 'service_id');
}
}
+6 -43
View File
@@ -4,56 +4,19 @@ namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class Skill extends Model
{
use HasFactory;
use HasFactory, HasTranslations;
protected $table = 'website_skills'; // نام جدول
protected $fillable = ['image', 'link', 'about_me_id', 'percentage']; // فیلدهایی که قابل پر کردن هستند
protected $table = 'website_skills';
protected $fillable = ['image', 'link', 'about_me_id', 'percentage'];
protected array $translatable = ['title', 'description'];
/**
* ارتباط با جدول AboutMe
*/
public function aboutMe()
{
return $this->belongsTo(AboutMe::class, 'about_me_id');
}
/**
* ارتباط با جدول ترجمه‌ها
*/
public function translations()
{
return $this->hasMany(SkillTranslation::class, 'skill_id');
}
/**
* گرفتن ترجمه بر اساس زبان فعلی
*/
public function getTranslation($locale)
{
if ($this->relationLoaded('translations')) {
return $this->translations->firstWhere('locale', $locale);
}
return $this->translations()->where('locale', $locale)->first();
}
/**
* گرفتن عنوان بر اساس زبان فعلی
*/
public function getTitle($locale)
{
$translation = $this->getTranslation($locale);
return $translation ? $translation->title : null;
}
/**
* گرفتن توضیحات بر اساس زبان فعلی
*/
public function getDescription($locale)
{
$translation = $this->getTranslation($locale);
return $translation ? $translation->description : null;
}
}
-22
View File
@@ -1,22 +0,0 @@
<?php
namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SkillTranslation extends Model
{
use HasFactory;
protected $table = 'website_skill_translations'; // نام جدول
protected $fillable = ['skill_id', 'locale', 'title', 'description']; // فیلدهایی که قابل پر کردن هستند
/**
* ارتباط با جدول Skills
*/
public function skill()
{
return $this->belongsTo(Skill::class, 'skill_id');
}
}