<?php

namespace App\Models;

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

class Way extends Model
{
    //
    use SoftDeletes;
    protected $fillable = [
        'image'
    ];
    public function descriptions($language_id= null)
    {

        if ($language_id) {
            $data = $this->hasMany(WayDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(WayDescription::class)->first();
            }

            return $data;
        }
        $data = $this->hasMany(WayDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }


    public static function withDescription($way_id = null)
    {
        $query = self::orderByDesc('ways.id')->join('way_descriptions as cd', 'cd.way_id', 'ways.id')
            ->where('cd.language_id', currentLanguage()->id)
            ->select(['ways.created_at', 'cd.*']);

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

        $query->select([
            'ways.*',
            'cd.name',
        ]);
        return $query;
    }
}
