阶段更新
This commit is contained in:
@@ -76,6 +76,11 @@ class Article extends Model
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
public function category_old(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CategoryOld::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: description
|
||||
* @Author: 玄尘
|
||||
|
||||
100
app/Models/ArticleOld.php
Normal file
100
app/Models/ArticleOld.php
Normal 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);
|
||||
// });
|
||||
}
|
||||
|
||||
}
|
||||
11
app/Models/ArticleOldCategory.php
Normal file
11
app/Models/ArticleOldCategory.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
class ArticleOldCategory extends Pivot
|
||||
{
|
||||
|
||||
//
|
||||
}
|
||||
@@ -6,10 +6,8 @@ use App\Models\Traits\HasCovers;
|
||||
use Encore\Admin\Traits\AdminBuilder;
|
||||
use Encore\Admin\Traits\ModelTree;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
@@ -114,8 +112,6 @@ class Category extends Model
|
||||
public function getDescriptionHtmlAttribute(): string
|
||||
{
|
||||
return str_replace("\n", "</br>", $this->description);
|
||||
|
||||
return Str::replaceArray('\n', ['</br>'], $this->description);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
119
app/Models/CategoryOld.php
Normal file
119
app/Models/CategoryOld.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\HasCovers;
|
||||
use Encore\Admin\Traits\AdminBuilder;
|
||||
use Encore\Admin\Traits\ModelTree;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CategoryOld extends Model
|
||||
{
|
||||
|
||||
use AdminBuilder, ModelTree, HasCovers;
|
||||
|
||||
public const TYPE_SHOW = 'show';
|
||||
public const TYPE_ARTICLE = 'article';
|
||||
public const TYPE_ADVERT = 'advert';
|
||||
public const TYPE_VIDEO = 'video';
|
||||
public const TYPES = [
|
||||
self::TYPE_ARTICLE => '文章列表',
|
||||
self::TYPE_SHOW => '文章详情',
|
||||
self::TYPE_ADVERT => '图片列表',
|
||||
self::TYPE_VIDEO => '视频列表',
|
||||
];
|
||||
|
||||
public $cover_field = 'cover';
|
||||
|
||||
public function getLogoUrlAttribute(): string
|
||||
{
|
||||
return $this->parseImageUrl($this->logo);
|
||||
}
|
||||
|
||||
public function getLinkAttribute(): string
|
||||
{
|
||||
return route('category.show', $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联的数据
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|\Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne|null [type] [description]
|
||||
*/
|
||||
public function relations()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case self::TYPE_SHOW:
|
||||
return $this->hasOne(Article::class)->where('id', $this->article_id);
|
||||
break;
|
||||
case self::TYPE_ARTICLE:
|
||||
return $this->belongsToMany(Article::class);
|
||||
break;
|
||||
case self::TYPE_ADVERT:
|
||||
return $this->hasMany(Advert::class);
|
||||
break;
|
||||
case self::TYPE_VIDEO:
|
||||
return $this->hasMany(Video::class);
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function childrens(): HasMany
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function parent(): HasOne
|
||||
{
|
||||
return $this->hasOne(self::class, 'id', 'parent_id');
|
||||
}
|
||||
|
||||
public function article(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Article::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取当前分类及子分类ID
|
||||
* @Author: <C.Jason>
|
||||
* @Date : 2020/4/6 3:12 下午
|
||||
* @return array
|
||||
*/
|
||||
public function getAllChildrenId(): array
|
||||
{
|
||||
$ids = array_keys($this->buildSelectOptions([], $this->id));
|
||||
if ($ids) {
|
||||
array_unshift($ids, $this->id);
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
// public function articles(): BelongsToMany
|
||||
// {
|
||||
// return $this->belongsToMany(Article::class);
|
||||
// }
|
||||
|
||||
public function articles(): HasMany
|
||||
{
|
||||
return $this->hasMany(Article::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 格式化description
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/10/8 15:24
|
||||
*/
|
||||
public function getDescriptionHtmlAttribute(): string
|
||||
{
|
||||
return str_replace("\n", "</br>", $this->description);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user