|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App; |
| 4 | + |
| 5 | +use App\Scopes\DraftScope; |
| 6 | +use App\Tools\Markdowner; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Database\Eloquent\SoftDeletes; |
| 9 | + |
| 10 | +class Article extends Model |
| 11 | +{ |
| 12 | + use SoftDeletes; |
| 13 | + |
| 14 | + /** |
| 15 | + * The attributes that should be mutated to dates. |
| 16 | + * |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + protected $dates = ['published_at', 'created_at', 'deleted_at']; |
| 20 | + |
| 21 | + /** |
| 22 | + * The attributes that are mass assignable. |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + protected $fillable = [ |
| 27 | + 'user_id', |
| 28 | + 'last_user_id', |
| 29 | + 'category_id', |
| 30 | + 'title', |
| 31 | + 'subtitle', |
| 32 | + 'slug', |
| 33 | + 'page_image', |
| 34 | + 'content', |
| 35 | + 'meta_description', |
| 36 | + 'is_draft', |
| 37 | + 'is_original', |
| 38 | + 'published_at', |
| 39 | + ]; |
| 40 | + |
| 41 | + protected $casts = [ |
| 42 | + 'content' => 'array' |
| 43 | + ]; |
| 44 | + |
| 45 | + /** |
| 46 | + * The "booting" method of the model. |
| 47 | + * |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public static function boot() |
| 51 | + { |
| 52 | + parent::boot(); |
| 53 | + |
| 54 | + static::addGlobalScope(new DraftScope()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the user for the blog article. |
| 59 | + * |
| 60 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 61 | + */ |
| 62 | + public function user() |
| 63 | + { |
| 64 | + return $this->belongsTo(User::class); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the category for the blog article. |
| 69 | + * |
| 70 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 71 | + */ |
| 72 | + public function category() |
| 73 | + { |
| 74 | + return $this->belongsTo(Category::class); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the tags for the blog article. |
| 79 | + * |
| 80 | + * @return \Illuminate\Database\Eloquent\Relations\morphToMany |
| 81 | + */ |
| 82 | + public function tags() |
| 83 | + { |
| 84 | + return $this->morphToMany(Tag::class, 'taggable'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the comments for the discussion. |
| 89 | + * |
| 90 | + * @return \Illuminate\Database\Eloquent\Relations\morphMany |
| 91 | + */ |
| 92 | + public function comments() |
| 93 | + { |
| 94 | + return $this->morphMany(Comment::class, 'commentable'); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the config for the configuration. |
| 99 | + * |
| 100 | + * @return \Illuminate\Database\Eloquent\Relations\morphMany |
| 101 | + */ |
| 102 | + public function config() |
| 103 | + { |
| 104 | + return $this->morphMany(Configuration::class, 'configuration'); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the created at attribute. |
| 109 | + * |
| 110 | + * @param $value |
| 111 | + * @return string |
| 112 | + */ |
| 113 | + public function getCreatedAtAttribute($value) |
| 114 | + { |
| 115 | + return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value)->diffForHumans(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Set the title and the readable slug. |
| 120 | + * |
| 121 | + * @param string $value |
| 122 | + */ |
| 123 | + public function setTitleAttribute($value) |
| 124 | + { |
| 125 | + $this->attributes['title'] = $value; |
| 126 | + |
| 127 | + if (!config('services.youdao.appKey') || !config('services.youdao.appSecret')) { |
| 128 | + $this->setUniqueSlug($value, str_random(5)); |
| 129 | + } else { |
| 130 | + $this->setUniqueSlug(translug($value), ''); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Set the unique slug. |
| 136 | + * |
| 137 | + * @param $value |
| 138 | + * @param $extra |
| 139 | + */ |
| 140 | + public function setUniqueSlug($value, $extra) { |
| 141 | + $slug = str_slug($value.'-'.$extra); |
| 142 | + |
| 143 | + if (static::whereSlug($slug)->exists()) { |
| 144 | + $this->setUniqueSlug($slug, (int) $extra + 1); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + $this->attributes['slug'] = $slug; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Set the content attribute. |
| 153 | + * |
| 154 | + * @param $value |
| 155 | + */ |
| 156 | + public function setContentAttribute($value) |
| 157 | + { |
| 158 | + $data = [ |
| 159 | + 'raw' => $value, |
| 160 | + 'html' => (new Markdowner)->convertMarkdownToHtml($value) |
| 161 | + ]; |
| 162 | + |
| 163 | + $this->attributes['content'] = json_encode($data); |
| 164 | + } |
| 165 | +} |
0 commit comments