parsaaghayi-backend/app/Models/Website/Service.php
parsa aghaei 75af8220a7
All checks were successful
Deploy Backend / deploy (push) Successful in 5m43s
1.3: fix N+1 in getTranslation by using eager-loaded collection
2026-06-23 18:10:22 +03:30

52 lines
1.3 KiB
PHP

<?php
namespace App\Models\Website;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Service extends Model
{
use HasFactory;
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;
}
}