文章对应分类多个
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Admin\Controllers\Article;
|
namespace App\Admin\Controllers\Article;
|
||||||
|
|
||||||
|
use App\Admin\Selectable\CategorySelectAble;
|
||||||
use App\Models\Article;
|
use App\Models\Article;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
use Encore\Admin\Controllers\AdminController;
|
use Encore\Admin\Controllers\AdminController;
|
||||||
@@ -20,7 +21,7 @@ class IndexController extends AdminController
|
|||||||
$grid->filter(function ($filter) {
|
$grid->filter(function ($filter) {
|
||||||
$filter->column(1 / 2, function ($filter) {
|
$filter->column(1 / 2, function ($filter) {
|
||||||
$filter->like('title', '文章标题');
|
$filter->like('title', '文章标题');
|
||||||
$filter->equal('category.id', '所属分类')->select(Category::selectOptions(function ($model) {
|
$filter->equal('categories.id', '所属分类')->select(Category::selectOptions(function ($model) {
|
||||||
return $model->where('status', 1)->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
return $model->where('status', 1)->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
||||||
}, '所有分类'));
|
}, '所有分类'));
|
||||||
});
|
});
|
||||||
@@ -30,7 +31,9 @@ class IndexController extends AdminController
|
|||||||
|
|
||||||
$grid->column('id', '#ID#');
|
$grid->column('id', '#ID#');
|
||||||
$grid->column('cover', '封面图片')->image('', 100);
|
$grid->column('cover', '封面图片')->image('', 100);
|
||||||
$grid->column('category.title', '所属分类');
|
$grid->column('所属分类')->display(function () {
|
||||||
|
return $this->categories()->pluck('title');
|
||||||
|
})->label();
|
||||||
$grid->column('title', '文章标题');
|
$grid->column('title', '文章标题');
|
||||||
$grid->column('sort', '序号');
|
$grid->column('sort', '序号');
|
||||||
$states = [
|
$states = [
|
||||||
@@ -48,15 +51,7 @@ class IndexController extends AdminController
|
|||||||
$form = new Form(new Article);
|
$form = new Form(new Article);
|
||||||
|
|
||||||
$form->text('title', '文章标题')->rules('min:2');
|
$form->text('title', '文章标题')->rules('min:2');
|
||||||
$form->select('category_id', '所属分类')
|
$form->belongsToMany('categories', CategorySelectAble::class, __('关联分类'));
|
||||||
->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->textarea('description', '内容简介')->rules('max:350');
|
$form->textarea('description', '内容简介')->rules('max:350');
|
||||||
|
|
||||||
$form->list('subjoin', '附加')
|
$form->list('subjoin', '附加')
|
||||||
|
|||||||
44
app/Admin/Selectable/CategorySelectAble.php
Normal file
44
app/Admin/Selectable/CategorySelectAble.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?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->column('type', '类型')->using(Category::TYPES);
|
||||||
|
|
||||||
|
$this->filter(function (Filter $filter) {
|
||||||
|
$filter->like('title', '分类名称');
|
||||||
|
$filter->equal('parent.id', '所属分类')
|
||||||
|
->select(Category::selectOptions(function ($model) {
|
||||||
|
return $model->where('status', 1)
|
||||||
|
->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
||||||
|
}, '所有分类'));
|
||||||
|
$filter->equal('type', '类型')->select(Category::TYPES);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -28,7 +28,11 @@ function getOneCategory($categoryId, $return = '')
|
|||||||
*/
|
*/
|
||||||
function getOneArticleBYCate($categoryId, $result = '')
|
function getOneArticleBYCate($categoryId, $result = '')
|
||||||
{
|
{
|
||||||
$info = Article::where('category_id', $categoryId)->latest('sort')->latest()->first();
|
$info = Article::where('status', 1)
|
||||||
|
->ByCategory($categoryId)
|
||||||
|
->latest('sort')
|
||||||
|
->latest()
|
||||||
|
->first();
|
||||||
|
|
||||||
if ($info) {
|
if ($info) {
|
||||||
if ($result) {
|
if ($result) {
|
||||||
@@ -54,8 +58,8 @@ function getOneArticleBYCate($categoryId, $result = '')
|
|||||||
function getArticlesBYCate($categoryId, $take = 8, $mark = 'one')
|
function getArticlesBYCate($categoryId, $take = 8, $mark = 'one')
|
||||||
{
|
{
|
||||||
if ($mark == 'one') {
|
if ($mark == 'one') {
|
||||||
$articles = Article::where('category_id', $categoryId)
|
$articles = Article::where('status', 1)
|
||||||
->where('status', 1)
|
->ByCategory($categoryId)
|
||||||
->latest('sort')
|
->latest('sort')
|
||||||
->latest()
|
->latest()
|
||||||
->take($take)
|
->take($take)
|
||||||
@@ -64,8 +68,8 @@ function getArticlesBYCate($categoryId, $take = 8, $mark = 'one')
|
|||||||
$cate = Category::find($categoryId);
|
$cate = Category::find($categoryId);
|
||||||
$ids = $cate->getAllChildrenId();
|
$ids = $cate->getAllChildrenId();
|
||||||
|
|
||||||
$articles = Article::whereIn('category_id', $ids)
|
$articles = Article::where('status', 1)
|
||||||
->where('status', 1)
|
->ByCategory($ids)
|
||||||
->latest('sort')
|
->latest('sort')
|
||||||
->latest()
|
->latest()
|
||||||
->take($take)
|
->take($take)
|
||||||
|
|||||||
@@ -13,10 +13,8 @@ class ArticleController extends Controller
|
|||||||
if ($article->url) {
|
if ($article->url) {
|
||||||
return redirect($article->url);
|
return redirect($article->url);
|
||||||
}
|
}
|
||||||
$category = $article->category;
|
|
||||||
$parent = $category->getTop();
|
|
||||||
|
|
||||||
return view('articles.show', compact('article', 'category', 'parent'));
|
return view('articles.show', compact('article'));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class CategoryController extends Controller
|
|||||||
/**
|
/**
|
||||||
* 显示分类
|
* 显示分类
|
||||||
* @param Category $category [description]
|
* @param Category $category [description]
|
||||||
* @return [type] [description]
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View [type] [description]
|
||||||
*/
|
*/
|
||||||
public function index(Category $category)
|
public function index(Category $category)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\Article;
|
use App\Models\Article;
|
||||||
|
use App\Models\ArticleCategory;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
use App\Models\DedeArchive;
|
use App\Models\DedeArchive;
|
||||||
use App\Models\DedeArctype;
|
use App\Models\DedeArctype;
|
||||||
@@ -10,6 +11,7 @@ use App\Traits\Tree;
|
|||||||
|
|
||||||
class TestController extends Controller
|
class TestController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
use Tree;
|
use Tree;
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
@@ -17,6 +19,22 @@ class TestController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function set_article_category()
|
||||||
|
{
|
||||||
|
$articles = Article::whereHas('category')
|
||||||
|
->chunk(200, function ($articles) {
|
||||||
|
foreach ($articles as $article) {
|
||||||
|
ArticleCategory::create([
|
||||||
|
'article_id' => $article->id,
|
||||||
|
'category_id' => $article->category_id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function setCateArticle()
|
public function setCateArticle()
|
||||||
{
|
{
|
||||||
$article = [];
|
$article = [];
|
||||||
@@ -58,7 +76,7 @@ class TestController extends Controller
|
|||||||
dump(count($oldids));
|
dump(count($oldids));
|
||||||
dump($diffids);
|
dump($diffids);
|
||||||
die();
|
die();
|
||||||
$map = [
|
$map = [
|
||||||
'id' => ['in', $diffids],
|
'id' => ['in', $diffids],
|
||||||
];
|
];
|
||||||
$list = DedeArchive::whereIn('id', $diffids)->get();
|
$list = DedeArchive::whereIn('id', $diffids)->get();
|
||||||
@@ -88,7 +106,7 @@ class TestController extends Controller
|
|||||||
dd('已经导入过数据');
|
dd('已经导入过数据');
|
||||||
}
|
}
|
||||||
$categorys = Category::get();
|
$categorys = Category::get();
|
||||||
$error = $success = [];
|
$error = $success = [];
|
||||||
DedeArchive::whereNotNull('litpic')->chunk(200, function ($articles) use ($categorys) {
|
DedeArchive::whereNotNull('litpic')->chunk(200, function ($articles) use ($categorys) {
|
||||||
|
|
||||||
foreach ($articles as $article) {
|
foreach ($articles as $article) {
|
||||||
@@ -128,7 +146,9 @@ class TestController extends Controller
|
|||||||
if ($categorys->count()) {
|
if ($categorys->count()) {
|
||||||
dd('已经导入过数据');
|
dd('已经导入过数据');
|
||||||
}
|
}
|
||||||
$lists = DedeArctype::where('ishidden', 0)->select('id', 'reid as parent_id', 'typename as title', 'content')->get();
|
$lists = DedeArctype::where('ishidden', 0)
|
||||||
|
->select('id', 'reid as parent_id', 'typename as title', 'content')
|
||||||
|
->get();
|
||||||
$list = Tree::list2tree($lists->toArray(), 'id', 'parent_id', 'children', 0);
|
$list = Tree::list2tree($lists->toArray(), 'id', 'parent_id', 'children', 0);
|
||||||
|
|
||||||
foreach ($list as $key => $value) {
|
foreach ($list as $key => $value) {
|
||||||
@@ -151,6 +171,7 @@ class TestController extends Controller
|
|||||||
'content' => $category['content'],
|
'content' => $category['content'],
|
||||||
'status' => 1,
|
'status' => 1,
|
||||||
];
|
];
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,12 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Models\Traits\BelongsToCategory;
|
use App\Models\Traits\BelongsToCategory;
|
||||||
use App\Models\Traits\HasOneCover;
|
use App\Models\Traits\HasOneCover;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
class Article extends Model
|
class Article extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
use HasOneCover, BelongsToCategory;
|
use HasOneCover;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应进行类型转换的属性
|
* 应进行类型转换的属性
|
||||||
@@ -23,4 +24,35 @@ class Article extends Model
|
|||||||
return route('article.show', $this);
|
return route('article.show', $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes: 关联分类
|
||||||
|
* @Author: 玄尘
|
||||||
|
* @Date : 2021/4/2 9:11
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||||
|
*/
|
||||||
|
public function categories(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Category::class)
|
||||||
|
->using(ArticleCategory::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->whereHas('categories', function ($q) use ($ids) {
|
||||||
|
$q->whereIN('id', $ids);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
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\AdminBuilder;
|
||||||
use Encore\Admin\Traits\ModelTree;
|
use Encore\Admin\Traits\ModelTree;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
class Category extends Model
|
class Category extends Model
|
||||||
{
|
{
|
||||||
@@ -36,7 +37,7 @@ class Category extends Model
|
|||||||
return $this->belongsTo(Article::class);
|
return $this->belongsTo(Article::class);
|
||||||
break;
|
break;
|
||||||
case self::TYPE_ARTICLE:
|
case self::TYPE_ARTICLE:
|
||||||
return $this->hasMany(Article::class);
|
return $this->belongsToMany(Article::class);
|
||||||
break;
|
break;
|
||||||
case self::TYPE_ADVERT:
|
case self::TYPE_ADVERT:
|
||||||
return $this->hasMany(Advert::class);
|
return $this->hasMany(Advert::class);
|
||||||
@@ -61,6 +62,11 @@ class Category extends Model
|
|||||||
return $this->belongsTo(Article::class);
|
return $this->belongsTo(Article::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function articles(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Article::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notes: 获取当前分类及子分类ID
|
* Notes: 获取当前分类及子分类ID
|
||||||
* @Author: <C.Jason>
|
* @Author: <C.Jason>
|
||||||
|
|||||||
@@ -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,43 +7,43 @@
|
|||||||
<!-- 详情 -->
|
<!-- 详情 -->
|
||||||
<div class="details">
|
<div class="details">
|
||||||
<div class="contant">
|
<div class="contant">
|
||||||
<div class="page-left">
|
{{-- <div class="page-left">--}}
|
||||||
<div class="pagelist">
|
{{-- <div class="pagelist">--}}
|
||||||
<h1>{{ $parent->title }}</h1>
|
{{-- <h1>{{ $parent->title }}</h1>--}}
|
||||||
<ul class="listbox">
|
{{-- <ul class="listbox">--}}
|
||||||
@if ($parent->children->isNotEmpty())
|
{{-- @if ($parent->children->isNotEmpty())--}}
|
||||||
@foreach ($parent->children as $children)
|
{{-- @foreach ($parent->children as $children)--}}
|
||||||
<li @if($category->id==$children->id) class="active" @endif>
|
{{-- <li @if($category->id==$children->id) class="active" @endif>--}}
|
||||||
<a href="{{ $children->link }}">{{ $children->title }}</a>
|
{{-- <a href="{{ $children->link }}">{{ $children->title }}</a>--}}
|
||||||
</li>
|
{{-- </li>--}}
|
||||||
@endforeach
|
{{-- @endforeach--}}
|
||||||
@endif
|
{{-- @endif--}}
|
||||||
</ul>
|
{{-- </ul>--}}
|
||||||
</div>
|
{{-- </div>--}}
|
||||||
</div>
|
{{-- </div>--}}
|
||||||
<div class="page-right">
|
{{-- <div class="page-right">--}}
|
||||||
<div class="pagelujing">
|
{{-- <div class="pagelujing">--}}
|
||||||
<div class="name">{{ $category->title }}</div>
|
{{-- <div class="name">{{ $category->title }}</div>--}}
|
||||||
<span>
|
{{-- <span>--}}
|
||||||
您当前所在位置:<a href="/">首页</a>
|
{{-- 您当前所在位置:<a href="/">首页</a>--}}
|
||||||
@if ($category->id!=$parent->id)
|
{{-- @if ($category->id!=$parent->id)--}}
|
||||||
> <a href="{{ $parent->link }}">{{ $parent->title }}</a> >
|
{{-- > <a href="{{ $parent->link }}">{{ $parent->title }}</a> >--}}
|
||||||
@endif
|
{{-- @endif--}}
|
||||||
> <a href="{{ $category->link }}">{{ $category->title }}</a>
|
{{-- > <a href="{{ $category->link }}">{{ $category->title }}</a>--}}
|
||||||
</span>
|
{{-- </span>--}}
|
||||||
</div>
|
{{-- </div>--}}
|
||||||
<div class="news-txt ny mg-t-b">
|
<div class="news-txt ny mg-t-b">
|
||||||
<div class="biaoti">{{ $article->title }}</div>
|
<div class="biaoti">{{ $article->title }}</div>
|
||||||
<div class="sshuomign"><span>发布时间:{{ $article->created_at->format('Y-m-d') }}</span></div>
|
<div class="sshuomign"><span>发布时间:{{ $article->created_at->format('Y-m-d') }}</span></div>
|
||||||
<div class="article_txt">
|
<div class="article_txt">
|
||||||
{{-- @if ($article->cover)--}}
|
{{-- @if ($article->cover)--}}
|
||||||
{{-- <img src="{{ $article->cover_path }}">--}}
|
{{-- <img src="{{ $article->cover_path }}">--}}
|
||||||
{{-- @endif--}}
|
{{-- @endif--}}
|
||||||
{!! $article->content !!}
|
{!! $article->content !!}
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{{-- </div>--}}
|
||||||
</div>
|
</div>
|
||||||
<!-- end 详情 -->
|
<!-- end 详情 -->
|
||||||
@endsection
|
@endsection
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ Route::get('articles/{article}', 'ArticleController@show')->name('article.show')
|
|||||||
Route::get('category/{category}', 'CategoryController@index')->name('category.show');
|
Route::get('category/{category}', 'CategoryController@index')->name('category.show');
|
||||||
|
|
||||||
//以下为导入数据
|
//以下为导入数据
|
||||||
|
Route::get('test/set_article_category', 'TestController@set_article_category');
|
||||||
Route::get('test/set_category', 'TestController@set_category');
|
Route::get('test/set_category', 'TestController@set_category');
|
||||||
Route::get('test/set_article', 'TestController@set_article');
|
Route::get('test/set_article', 'TestController@set_article');
|
||||||
Route::get('test/set_cate_article', 'TestController@setCateArticle');
|
Route::get('test/set_cate_article', 'TestController@setCateArticle');
|
||||||
|
|||||||
Reference in New Issue
Block a user