阶段性更新
This commit is contained in:
@@ -3,13 +3,11 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\BelongsToCategory;
|
||||
use App\Models\Traits\HasOneCover;
|
||||
use App\Models\Traits\OrderByIdDesc;
|
||||
use App\Scopes\SortScope;
|
||||
use App\Models\Traits\HasCovers;
|
||||
|
||||
class Advert extends Model
|
||||
{
|
||||
|
||||
use HasOneCover,
|
||||
use HasCovers,
|
||||
BelongsToCategory;
|
||||
}
|
||||
|
||||
@@ -3,19 +3,26 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\BelongsToCategory;
|
||||
use App\Models\Traits\HasOneCover;
|
||||
use App\Models\Traits\HasCovers;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Article extends Model
|
||||
{
|
||||
|
||||
use HasOneCover, BelongsToCategory;
|
||||
use HasCovers, BelongsToCategory;
|
||||
|
||||
public function getLinkAttribute()
|
||||
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);
|
||||
@@ -35,12 +42,11 @@ class Article extends Model
|
||||
*/
|
||||
public function get_one_cover()
|
||||
{
|
||||
if ($this->cover_path) {
|
||||
$path = $this->cover_path;
|
||||
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;
|
||||
@@ -65,6 +71,11 @@ class Article extends Model
|
||||
->using(ArticleCategory::class);
|
||||
}
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: description
|
||||
* @Author: 玄尘
|
||||
@@ -79,9 +90,11 @@ class Article extends Model
|
||||
$ids = [$ids];
|
||||
}
|
||||
|
||||
return $query->whereHas('categories', function ($q) use ($ids) {
|
||||
$q->whereIN('id', $ids);
|
||||
});
|
||||
return $query->whereIn('category_id', $ids);
|
||||
//
|
||||
// return $query->whereHas('categories', function ($q) use ($ids) {
|
||||
// $q->whereIN('id', $ids);
|
||||
// });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,32 +2,46 @@
|
||||
|
||||
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 Category extends Model
|
||||
{
|
||||
|
||||
use AdminBuilder, ModelTree;
|
||||
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_ADVERT => '图片列表',
|
||||
self::TYPE_VIDEO => '视频列表',
|
||||
];
|
||||
|
||||
public function getLinkAttribute()
|
||||
public $cover_field = 'cover';
|
||||
|
||||
public function getLogoUrlAttribute(): string
|
||||
{
|
||||
return $this->parseImageUrl($this->logo);
|
||||
}
|
||||
|
||||
public function getLinkAttribute(): string
|
||||
{
|
||||
return route('category.show', $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联的数据
|
||||
* @return [type] [description]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|\Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne|null [type] [description]
|
||||
*/
|
||||
public function relations()
|
||||
{
|
||||
@@ -41,22 +55,25 @@ class Category extends Model
|
||||
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()
|
||||
public function childrens(): HasMany
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function parent()
|
||||
public function parent(): HasOne
|
||||
{
|
||||
return $this->hasOne(self::class, 'id', 'parent_id');
|
||||
}
|
||||
|
||||
public function article()
|
||||
public function article(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Article::class);
|
||||
}
|
||||
@@ -67,17 +84,38 @@ class Category extends Model
|
||||
* @Date : 2020/4/6 3:12 下午
|
||||
* @return array
|
||||
*/
|
||||
public function getAllChildrenId()
|
||||
public function getAllChildrenId(): array
|
||||
{
|
||||
$ids = array_keys($this->buildSelectOptions([], $this->id));
|
||||
array_unshift($ids, $this->id);
|
||||
if ($ids) {
|
||||
array_unshift($ids, $this->id);
|
||||
|
||||
return $ids;
|
||||
return $ids;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function articles(): BelongsToMany
|
||||
// public function articles(): BelongsToMany
|
||||
// {
|
||||
// return $this->belongsToMany(Article::class);
|
||||
// }
|
||||
|
||||
public function articles(): HasMany
|
||||
{
|
||||
return $this->belongsToMany(Article::class);
|
||||
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);
|
||||
|
||||
return Str::replaceArray('\n', ['</br>'], $this->description);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,22 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Encore\Admin\Traits\DefaultDatetimeFormat;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
|
||||
class Model extends Eloquent
|
||||
{
|
||||
|
||||
use DefaultDatetimeFormat;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* 为数组 / JSON 序列化准备日期。
|
||||
*
|
||||
* @param \DateTimeInterface $date
|
||||
* @return string
|
||||
*/
|
||||
protected function serializeDate(DateTimeInterface $date)
|
||||
{
|
||||
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
83
app/Models/Traits/HasCovers.php
Normal file
83
app/Models/Traits/HasCovers.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait HasCovers
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes : 获取封面图片字段(单图)
|
||||
* @Date : 2021/3/16 4:34 下午
|
||||
* @Author : < Jason.C >
|
||||
* @return string
|
||||
*/
|
||||
public function getCoverField(): string
|
||||
{
|
||||
return $this->cover_field ?? 'cover';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取图片字段(多图)
|
||||
* @Date : 2021/3/16 4:35 下午
|
||||
* @Author : < Jason.C >
|
||||
* @return string
|
||||
*/
|
||||
public function getPicturesField(): string
|
||||
{
|
||||
return $this->pictures_field ?? 'pictures';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 解析单图地址
|
||||
* @Date : 2021/3/16 4:54 下午
|
||||
* @Author : < Jason.C >
|
||||
* @return string
|
||||
*/
|
||||
public function getCoverUrlAttribute(): string
|
||||
{
|
||||
$cover = $this->getAttribute($this->getCoverField());
|
||||
|
||||
return $this->parseImageUrl($cover);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 解析多图地址
|
||||
* @Date : 2021/3/16 4:54 下午
|
||||
* @Author : < Jason.C >
|
||||
* @return array
|
||||
*/
|
||||
public function getPicturesUrlAttribute(): array
|
||||
{
|
||||
$pictures = $this->getAttribute($this->getPicturesField());
|
||||
|
||||
if (empty($pictures)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return collect($pictures)->map(function ($picture) {
|
||||
return $this->parseImageUrl($picture);
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 解析图片文件的实际展示地址
|
||||
* @Date : 2021/3/16 4:53 下午
|
||||
* @Author : < Jason.C >
|
||||
* @param string|null $image
|
||||
* @return string
|
||||
*/
|
||||
protected function parseImageUrl(?string $image): string
|
||||
{
|
||||
if (empty($image)) {
|
||||
return '';
|
||||
} elseif (Str::startsWith($image, 'http')) {
|
||||
return $image;
|
||||
} else {
|
||||
return Storage::url($image);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
trait HasOneCover
|
||||
{
|
||||
|
||||
/**
|
||||
* 拼接图片全地址
|
||||
* @author 玄尘 2020-03-05
|
||||
* @return string
|
||||
*/
|
||||
public function getCoverPathAttribute(): ?string
|
||||
{
|
||||
if ($this->cover) {
|
||||
return Storage::url($this->cover);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
13
app/Models/Video.php
Normal file
13
app/Models/Video.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\BelongsToCategory;
|
||||
use App\Models\Traits\HasCovers;
|
||||
|
||||
class Video extends Model
|
||||
{
|
||||
|
||||
use HasCovers,
|
||||
BelongsToCategory;
|
||||
}
|
||||
Reference in New Issue
Block a user