HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/fire-tech/fire-tech.nl/app/KommaApp/Kms/Core/AbstractTranslatableModel.php
<?php
namespace App\KommaApp\Kms\Core;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\App;

/**
 * Represents a eloquent model that has a translation method to retrieve translations
 *
 * Interface TranslatableModelInterface
 * @package App\KommaApp\Kms\Core
 */
abstract class AbstractTranslatableModel extends Model
{
    //Make also sure that your model has a title attribute! Used in section header

    /**
     * Gets the translation models for this model
     *
     * @return HasMany that resolves to AbstractTranslationModel instances
     */
    abstract public function translations(): HasMany;

    /**
     * Looks at the app language and searches the matching translation model for it.
     *
     * @throws \RuntimeException
     */
    public function translation(): HasOne
    {
        $languageId = App::getLanguage()->id;
        $translationsModel = get_class($this->translations()->getRelated());
        return $this->hasOne($translationsModel)->where('language_id', '=', $languageId);
    }
}