调整分类
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Admin\Controllers\Article;
|
||||
|
||||
use App\Admin\Selectable\CategorySelectAble;
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
@@ -33,7 +34,9 @@ class IndexController extends AdminController
|
||||
|
||||
$grid->column('id', '#ID#');
|
||||
$grid->column('cover', '封面图片')->image('', 100);
|
||||
$grid->column('category.title', '所属分类');
|
||||
$grid->column('所属分类')->display(function () {
|
||||
return $this->categories()->pluck('title');
|
||||
})->label();
|
||||
$grid->column('title', '文章标题');
|
||||
$grid->column('sort', '序号');
|
||||
$states = [
|
||||
@@ -51,20 +54,16 @@ class IndexController extends AdminController
|
||||
$form = new Form(new Article);
|
||||
|
||||
$form->text('title', '文章标题')->rules('min:2');
|
||||
$form->select('category_id', '所属分类')
|
||||
->options(Category::selectOptions(function ($model) {
|
||||
return $model->where('status', 1)->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
||||
}, '选择分类'))
|
||||
->rules('required|min:1', [
|
||||
'required' => '必须选择所属分类',
|
||||
'min' => '必须选择所属分类',
|
||||
]);
|
||||
|
||||
$form->belongsToMany('categories', CategorySelectAble::class, __('关联分类'));
|
||||
|
||||
$form->textarea('description', '内容简介');
|
||||
$form->text('tenure', '任期')->help('领导班子需要添加此数据');
|
||||
$form->image('cover', '封面')
|
||||
->move('images/' . date('Y/m/d'))
|
||||
->removable()
|
||||
->uniqueName();
|
||||
|
||||
$form->ueditor('content', '文章内容')->rules('required', ['required' => '详情不能为空']);
|
||||
$form->number('sort', '序号')->default(0)->rules('required', ['required' => '序号必须填写'])->help('倒序优先');
|
||||
$states = [
|
||||
@@ -75,6 +74,22 @@ class IndexController extends AdminController
|
||||
|
||||
$form->switch('status', '状态')->states($states)->default(1);
|
||||
|
||||
// $form->saved(function (Form $form) {
|
||||
// $category_ids = request()->category_ids;
|
||||
// if ($category_ids) {
|
||||
// $categories = [];
|
||||
// foreach ($category_ids as $key => $category_id) {
|
||||
// if ($category_id) {
|
||||
// $form->model()->article_categories()->updateOrCreate([
|
||||
// 'category_id' => $category_id,
|
||||
// ]);
|
||||
// $categories[] = $category_id;
|
||||
// }
|
||||
// }
|
||||
// $form->model()->article_categories()->whereNotIn('category_id', $categories)->delete();
|
||||
// }
|
||||
// });
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,9 +37,11 @@ class IndexController extends AdminController
|
||||
$form->select('type', '分类类型')
|
||||
->options(Category::TYPES)
|
||||
->when('show', function (WidgetsForm $form) {
|
||||
// $form->text('article_id', '关联文章');
|
||||
|
||||
$form->select('article_id', '关联文章')
|
||||
->options(function ($option, $info) {
|
||||
return Article::whereHas('category', function ($q) {
|
||||
return Article::whereHas('categories', function ($q) {
|
||||
$q->where('type', 'show');
|
||||
})->pluck('title', 'id');
|
||||
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
||||
@@ -103,7 +105,7 @@ class IndexController extends AdminController
|
||||
->when('show', function (Form $form) {
|
||||
$form->select('article_id', '关联文章')
|
||||
->options(function ($option, $info) {
|
||||
return Article::whereHas('category', function ($q) {
|
||||
return Article::whereHas('categories', function ($q) {
|
||||
$q->where('type', 'show');
|
||||
})->pluck('title', 'id');
|
||||
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
||||
|
||||
37
app/Admin/Selectable/CategorySelectAble.php
Normal file
37
app/Admin/Selectable/CategorySelectAble.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Selectable;
|
||||
|
||||
use App\Models\Category;
|
||||
use Encore\Admin\Grid\Filter;
|
||||
use Encore\Admin\Grid\Selectable;
|
||||
|
||||
class CategorySelectAble extends Selectable
|
||||
{
|
||||
|
||||
public $model = Category::class;
|
||||
|
||||
public static function display()
|
||||
{
|
||||
return function ($value) {
|
||||
if (is_array($value)) {
|
||||
return implode(';', array_column($value, 'title'));
|
||||
}
|
||||
|
||||
return optional($this->categories)->title;
|
||||
};
|
||||
}
|
||||
|
||||
public function make()
|
||||
{
|
||||
$this->model()->where('status', 1);
|
||||
|
||||
$this->column('id', 'ID');
|
||||
$this->column('title', '分类名称');
|
||||
|
||||
$this->filter(function (Filter $filter) {
|
||||
$filter->like('title', '分类名称');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,7 +49,12 @@ function getOneCategory($categoryId, $return = '')
|
||||
*/
|
||||
function getOneArticleBYCate($categoryId, $result = '')
|
||||
{
|
||||
$info = Article::where('category_id', $categoryId)->latest('sort')->latest()->first();
|
||||
$info = Article::latest('sort')
|
||||
->whereHas('categories', function ($q) use ($categoryId) {
|
||||
$q->where('id', $categoryId);
|
||||
})
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if ($info) {
|
||||
if ($result) {
|
||||
@@ -78,8 +83,10 @@ function getOneArticleBYCate($categoryId, $result = '')
|
||||
function getArticlesBYCate($categoryId, $take = 8, $mark = 'one', $hasCover = false, $order = 'desc')
|
||||
{
|
||||
if ($mark == 'one') {
|
||||
$articles = Article::where('category_id', $categoryId)
|
||||
->where('status', 1)
|
||||
$articles = Article::where('status', 1)
|
||||
->whereHas('categories', function ($q) use ($categoryId) {
|
||||
$q->where('id', $categoryId);
|
||||
})
|
||||
->orderBy('sort', $order)
|
||||
->when($hasCover, function ($q) {
|
||||
$q->whereNotNull('cover');
|
||||
@@ -90,8 +97,10 @@ function getArticlesBYCate($categoryId, $take = 8, $mark = 'one', $hasCover = fa
|
||||
$cate = Category::find($categoryId);
|
||||
$ids = $cate->getAllChildrenId();
|
||||
|
||||
$articles = Article::whereIn('category_id', $ids)
|
||||
->where('status', 1)
|
||||
$articles = Article::where('status', 1)
|
||||
->whereHas('categories', function ($q) use ($ids) {
|
||||
$q->whereIN('id', $ids);
|
||||
})
|
||||
->when($hasCover, function ($q) {
|
||||
$q->whereNotNull('cover');
|
||||
})
|
||||
@@ -157,11 +166,13 @@ function getArticlesByCateIds($take = 8, $hasCover = false)
|
||||
{
|
||||
$ids = [7, 17, 8, 5, 12, 30];
|
||||
|
||||
$articles = Article::whereIn('category_id', $ids)
|
||||
->where('status', 1)
|
||||
$articles = Article::where('status', 1)
|
||||
->whereHas('categories', function ($q) use ($ids) {
|
||||
$q->whereIN('id', $ids);
|
||||
})
|
||||
->latest()
|
||||
->when($hasCover, function ($q) {
|
||||
$q->whereNotNull('cover');
|
||||
$q->whereNotNull('cover')->orWhere('cover', '<>', '');
|
||||
})
|
||||
->take($take)
|
||||
->get();
|
||||
|
||||
@@ -16,17 +16,15 @@ class ArticleController extends Controller
|
||||
*/
|
||||
public function show(Article $article)
|
||||
{
|
||||
$parent = $category = $article->category;
|
||||
if ($category->id) {
|
||||
$parent = getTopCate($category->id);
|
||||
}
|
||||
$categories = $article->categories;
|
||||
$next = Article::where('id', '>', $article->id)
|
||||
->whereHas('categories', function ($q) use ($categories) {
|
||||
$q->whereIN('id', $categories->pluck('id'));
|
||||
})
|
||||
->where('status', 1)
|
||||
->first();
|
||||
|
||||
$next = Article::where('id', '>', $article->id)
|
||||
->where('category_id', $article->category_id)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
|
||||
return view('article.show', compact('article', 'next', 'parent', 'category'));
|
||||
return view('article.show', compact('article', 'next'));
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +36,6 @@ class ArticleController extends Controller
|
||||
->when($title, function ($q) use ($title) {
|
||||
$q->where('title', 'like', "%{$title}%");
|
||||
})
|
||||
->where('category_id', '>', 0)
|
||||
->paginate();
|
||||
|
||||
return view('article.search', compact('articles'));
|
||||
|
||||
@@ -16,26 +16,17 @@ class IndexController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$fydt = $this->getArticle([6], 9); //分院动态
|
||||
$kydt = $this->getArticle([9], 9); //科研动态
|
||||
$ldbz = $this->getArticle([3], 4); //领导班子
|
||||
$kycg = $this->getArticle([10], 8); //科研成果
|
||||
$rctd = $this->getArticle([4], 9); //人才团队介绍
|
||||
$info = Article::where('category_id', 2)->first(); //院所介绍
|
||||
$links = Link::get();
|
||||
|
||||
$advert = Advert::where('category_id', 26)->latest('sort')->first();
|
||||
$center_advert = Advert::where('category_id', 28)->latest('sort')->first();
|
||||
|
||||
return view('index.index', compact('advert', 'center_advert', 'fydt', 'kydt', 'ldbz', 'kycg', 'rctd', 'info'));
|
||||
return view('index.index');
|
||||
}
|
||||
|
||||
//通用获取文章
|
||||
public function getArticle($category_ids, $take = 3)
|
||||
{
|
||||
return Article::whereIn('category_id', $category_ids)
|
||||
return Article::latest('sort')
|
||||
->whereHas('categories', function ($q) use ($category_ids) {
|
||||
$q->where('id', $category_ids);
|
||||
})
|
||||
->select('id', 'description', 'title', 'created_at', 'cover', 'content')
|
||||
->latest('sort')
|
||||
->latest()
|
||||
->take($take)
|
||||
->get();
|
||||
|
||||
@@ -4,18 +4,21 @@ namespace App\Models;
|
||||
|
||||
use App\Models\Traits\BelongsToCategory;
|
||||
use App\Models\Traits\HasOneCover;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class Article extends Model
|
||||
{
|
||||
|
||||
use HasOneCover, BelongsToCategory;
|
||||
use HasOneCover;
|
||||
|
||||
public function getLinkAttribute()
|
||||
public function getLinkAttribute(): string
|
||||
{
|
||||
return route('article.show', $this);
|
||||
}
|
||||
|
||||
public function get_content_cover()
|
||||
|
||||
public function get_content_cover(): string
|
||||
{
|
||||
preg_match("/<img.*?src=\"([^\"]+)\"[^>].*?>/isU", str_ireplace("\\", "", $this->content), $matches);
|
||||
|
||||
@@ -26,6 +29,22 @@ class Article extends Model
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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: 玄尘
|
||||
|
||||
11
app/Models/ArticleCategory.php
Normal file
11
app/Models/ArticleCategory.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
class ArticleCategory extends Pivot
|
||||
{
|
||||
|
||||
//
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Encore\Admin\Traits\AdminBuilder;
|
||||
use Encore\Admin\Traits\ModelTree;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
@@ -35,7 +36,7 @@ class Category extends Model
|
||||
return $this->belongsTo(Article::class);
|
||||
break;
|
||||
case self::TYPE_ARTICLE:
|
||||
return $this->hasMany(Article::class);
|
||||
return $this->belongsToMany(Article::class);
|
||||
break;
|
||||
case self::TYPE_ADVERT:
|
||||
return $this->hasMany(Advert::class);
|
||||
@@ -55,9 +56,9 @@ class Category extends Model
|
||||
return $this->hasOne(self::class, 'id', 'parent_id');
|
||||
}
|
||||
|
||||
public function article()
|
||||
public function articles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsTo(Article::class);
|
||||
return $this->belongsToMany(Article::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateArticleCategoriesTable extends Migration
|
||||
{
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('article_category', function (Blueprint $table) {
|
||||
$table->foreignId('article_id');
|
||||
$table->foreignId('category_id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('article_categories');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,26 +7,25 @@
|
||||
<div class="survey" style="background-image: url(/assets/index/images/bannerBack.jpg);">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
@include('category.left')
|
||||
<div class="col-xs-12 col-md-9 secondRight">
|
||||
<div class="col-xs-12 col-md-12 secondRight">
|
||||
<!-- 分院新闻 -->
|
||||
<div class="surveyCont">
|
||||
<div class="secondTop">
|
||||
<div class="secondTop-name">
|
||||
<img src="/assets/index/images/newsIcon_00.png"/>
|
||||
{{ $category->title }}
|
||||
</div>
|
||||
{{-- <div class="surveyCont">--}}
|
||||
{{-- <div class="secondTop">--}}
|
||||
{{-- <div class="secondTop-name">--}}
|
||||
{{-- <img src="/assets/index/images/newsIcon_00.png"/>--}}
|
||||
{{-- {{ $category->title }}--}}
|
||||
{{-- </div>--}}
|
||||
{{-- </div>--}}
|
||||
<div class="Details">
|
||||
<div class="Details-title">
|
||||
{{ $article->title }}
|
||||
</div>
|
||||
<div class="Details">
|
||||
<div class="Details-title">
|
||||
{{ $article->title }}
|
||||
</div>
|
||||
<div class="Details-text">
|
||||
{!! $article->content !!}
|
||||
<div class="Details-text">
|
||||
{!! $article->content !!}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{-- </div>--}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user