<?php

namespace App;

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

class BlogCategorie extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'language_id', 'slug', 'name', 'display_order'
    ];

    public function blogpost()
    {
        return $this->hasMany('App\BlogPost');
    }

    public static function existeSlug($slug, $id)
    {

        if ($id != 0) {
            $existe = BlogCategorie::where('slug' , '=', $slug)
                ->where('id', '<>', $id)->get();
        } else {
            $existe = BlogCategorie::where('slug', '=', $slug)->get();
        }

        if (count($existe) > 0) {
            $slug = $slug . (count($existe));
        }

        return $slug;
    }

}
