<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Slider extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'image','link','status'
    ];


    public static function withDescription($slider_id = null)
    {
        $query = self::orderByDesc('sliders.id')->join('slider_descriptions as sd', 'sd.slider_id', 'sliders.id')
            ->where('sd.language_id', currentLanguage()->id)
            ->select(['sliders.created_at', 'sd.*']);
        if ($slider_id){
            $query->where('sliders.id', $slider_id);
        }

        $query->select([
            'sliders.*',
            'sd.name',
            'sd.description',
        ]);
        return $query;
    }
    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(SliderDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(SliderDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(SliderDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }
}
