<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Nationality extends Model
{
    //
    use SoftDeletes;
    protected $fillable = [
        'key','iso'
    ];

    public static function withDescription($nationality_id = null)
    {
        $query = self::join('nationality_descriptions as cd', 'cd.nationality_id', 'nationalities.id')
            ->where('cd.language_id', currentLanguage()->id)
            ->select(['nationalities.*', 'cd.name'])->orderBy('cd.name');

        if ($nationality_id)
        {
            if (is_array($nationality_id))
            {
                $query->whereIn('nationalities.id', $nationality_id);
            }
            else
            {
                $query->where('nationalities.id', $nationality_id);
            }
        }

        $query->select([
            'nationalities.*',
            'cd.name',
        ]);
        return $query;
    }
    public function currentDescription()
    {
        return $this->hasOne(NationalityDescription::class)->where('language_id',currentLanguage()['id']);
    }
}
