77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Traits\HasOneCover;
|
|
use App\Models\Traits\WithPosition;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Article extends Model
|
|
{
|
|
|
|
use HasOneCover,
|
|
WithPosition;
|
|
|
|
const POSITIONS = [
|
|
1 => '首页推荐',
|
|
];
|
|
|
|
public function getLinkAttribute(): string
|
|
{
|
|
return route('article.show', $this);
|
|
}
|
|
|
|
public function get_content_cover(): string
|
|
{
|
|
preg_match("/<img.*?src=\"([^\"]+)\"[^>].*?>/isU", str_ireplace("\\", "", $this->content), $matches);
|
|
|
|
if (isset($matches[1])) {
|
|
return $matches[1];
|
|
} else {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes: 关联分类的中间表
|
|
* @Author: 玄尘
|
|
* @Date : 2021/4/1 15:19
|
|
*/
|
|
public function article_categories(): HasMany
|
|
{
|
|
return $this->hasMany(ArticleCategory::class);
|
|
}
|
|
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class)
|
|
->using(ArticleCategory::class);
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取一个默认图片
|
|
* @Author: 玄尘
|
|
* @Date : 2020/6/3 16:29
|
|
* @return mixed|string
|
|
*/
|
|
public function get_one_cover()
|
|
{
|
|
if ($this->cover_path) {
|
|
$path = $this->cover_path;
|
|
} else {
|
|
$path = $this->get_content_cover();
|
|
// if ($path) {
|
|
// $this->cover = str_replace("/storage", "", $path);
|
|
// $this->save();
|
|
// }
|
|
$path = config('app.url') . $path;
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|