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/SBogers10/honger.komma.pro/app/KommaApp/Cases/Models/CaseModel.php
<?php
/**
 * Created by PhpStorm.
 * User: mike
 * Date: 18/05/17
 * Time: 22:00
 */

namespace App\KommaApp\Cases\Models;


use App\KommaApp\DisplayNameTrait;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNodeInterface;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNodeLogicTrait;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TranslatableEloquentNode;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Pages\Models\PageTranslation;
use App\KommaApp\Sites\Models\Site;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\App;

class CaseModel extends TranslatableEloquentNode implements EloquentNodeInterface
{
    use EloquentNodeLogicTrait;
    use DisplayNameTrait;

    protected $table = 'cases';
    protected $kmsClass = CaseModel::class;

    public $thumbnail = false;

    protected $fillable = ['active', 'site_id', 'lft', 'rgt', 'tree', 'code_name'];

    public function site(): BelongsTo
    {
        return $this->belongsTo(Site::class);
    }

    /**
     * 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, 'case_id', 'id')->where('language_id', '=', $languageId);
    }


    /**
     * Gets the translation models for this model
     *
     * @return HasMany that resolves to AbstractTranslationModel instances
     */
    public function translations(): HasMany
    {
        return $this->hasMany(CaseTranslation::class, 'case_id', 'id');
    }

    public function languages(): BelongsToMany
    {
        return $this->belongsToMany(Language::class, 'case_translations', 'case_id')
            ->withPivot('slug', 'name')
            ->withTimestamps();
    }

    /**
     * Get the images from the current user
     *
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
     */
    public function images(): HasMany
    {
        /**
         *
         * On the Image model is an MorphTo relation
         * By using a hasMany relation:
         * where the imageable_type is filled in with the KmsClass
         * And the imageable_id is set as the foreign_id,
         * we can collect the images of the given model directly.
         *
         */
        return $this->hasMany(Image::class, 'imageable_id')
            ->where('imageable_type', '=', $this->kmsClass);
    }

    public function __get($key)
    {
        if($key == "title" && isset($this->translation)) {
            return $this->translation->name;
        }

        if($key == "parent_id") {
            if($this::find($this->id))
                return $this->getParentId();
        }

        return parent::__get($key);
    }

}