阶段更新

This commit is contained in:
2021-10-11 13:09:29 +08:00
parent d89bc90cef
commit 11b9e2ae76
31 changed files with 1095 additions and 659 deletions

100
app/Models/ArticleOld.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
namespace App\Models;
use App\Models\Traits\BelongsToCategory;
use App\Models\Traits\HasCovers;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class ArticleOld extends Model
{
use HasCovers, BelongsToCategory;
public function getLinkAttribute(): string
{
return route('article.show', $this);
}
/***
* Notes: 获取详情内图片
* @Author: 玄尘
* @Date : 2021/10/8 11:58
* @return mixed|string
*/
public function get_content_cover()
{
preg_match("/<img.*?src=\"([^\"]+)\"[^>].*?>/isU", str_ireplace("\\", "", $this->content), $matches);
if (isset($matches[1])) {
return $matches[1];
} else {
return '';
}
}
/**
* Notes: 获取一个默认图片
* @Author: 玄尘
* @Date : 2020/6/3 16:29
* @return mixed|string
*/
public function get_one_cover()
{
if ($this->cover_url) {
$path = $this->cover_url;
} else {
$path = $this->get_content_cover();
if ($path) {
$this->cover = str_replace("/storage", "", $path);
$this->save();
$path = config('app.url') . $path;
}
}
return $path;
}
/**
* Notes: 关联分类
* @Author: 玄尘
* @Date : 2021/4/2 9:11
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categories(): BelongsToMany
{
return $this->belongsToMany(CategoryOld::class, 'article_old_category')
->using(ArticleOldCategory::class);
}
public function category(): BelongsTo
{
return $this->belongsTo(CategoryOld::class);
}
/**
* Notes: description
* @Author: 玄尘
* @Date : 2021/4/2 9:17
* @param $query
* @param $ids
* @return mixed
*/
public function scopeByCategory($query, $ids)
{
if (!is_array($ids)) {
$ids = [$ids];
}
return $query->whereIn('category_id', $ids);
//
// return $query->whereHas('categories', function ($q) use ($ids) {
// $q->whereIN('id', $ids);
// });
}
}