<?php

namespace App;

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

class BlogPost extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'language_id','slug', 'title', 'subtitle','content', 'date_published', 'hour_published',
        'visits', 'status', 'highlight', 'seo_title', 'seo_description', 'tags',
        'tipo', 'autor_id', 'blogcategories_id', 'imagem', 'background', 'arquivo', 'btnfile', 'alt_image', 'meta_description', 'meta_title', 'likes'

    ];

    public function blogcategory()
    {
      return $this->belongsTo('App\BlogCategorie');
    }

    public function blogCta()
    {
      return $this->belongsTo('App\BlogCta');
    }


    // public function blogTags()
    // {
    //   return $this->belongsToMany('App\BlogTag');
    // }


    public function author()
    {
      return $this->belongsTo('App\Author');
    }


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

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

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

        return $slug;
    }

    // public function adicionarBlogTag($tag){
    //     if (is_string($tag)) {
    //       $tag = BlogTag::where('name', '=', $tag)->firstOrFail();
    //     }
  
    //     if ($this->existeBlogTag($tag)){
    //       return;
    //     }
  
    //     return $this->blogTags()->attach($tag); //attach - adiciona um relacionamento
  
    //   } 
  
      // public function existeBlogTag($tag){
      //     if (is_string($tag)){
      //         $tag = BlogTag::where('name', '=', $tag)->firstOrFail();
      //     }
          
      //     return (boolean) $this->blogTags()->find($tag->id);
      // }
  
      // public function removeBlogTag($tag){
      //     if (is_string($tag)){
      //         $tag = BlogTag::where('name', '=', $tag)->firstOrFail();
      //     }
  
      //     return $this->blogTags()->detach($tag); //detach - remove um relacionamento
      // }

      // public function removeAllBlogTags(){
            
      //     return $this->blogtag()->detach(); //detach - remove um relacionamento
      // }

    
}