diff --git a/app/Admin/Controllers/Article/IndexController.php b/app/Admin/Controllers/Article/IndexController.php index 15f6750..c8aee69 100644 --- a/app/Admin/Controllers/Article/IndexController.php +++ b/app/Admin/Controllers/Article/IndexController.php @@ -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; @@ -20,7 +21,7 @@ class IndexController extends AdminController $grid->filter(function ($filter) { $filter->column(1 / 2, function ($filter) { $filter->like('title', '文章标题'); - $filter->like('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]); }, '所有分类')); }); @@ -30,7 +31,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', '文章标题'); $states = [ 'on' => ['value' => 1, 'text' => '打开', 'color' => 'primary'], @@ -48,14 +51,8 @@ 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->image('cover', '封面') ->move('images/' . date('Y/m/d')) diff --git a/app/Admin/Controllers/Category/IndexController.php b/app/Admin/Controllers/Category/IndexController.php index 505098b..2201fa9 100644 --- a/app/Admin/Controllers/Category/IndexController.php +++ b/app/Admin/Controllers/Category/IndexController.php @@ -38,7 +38,7 @@ class IndexController extends AdminController ->when('show', function (WidgetsForm $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('当分类类型是文章详情的时候需要选择关联文章'); @@ -101,7 +101,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('当分类类型是文章详情的时候需要选择关联文章'); diff --git a/app/Admin/Selectable/CategorySelectAble.php b/app/Admin/Selectable/CategorySelectAble.php new file mode 100644 index 0000000..8af4db9 --- /dev/null +++ b/app/Admin/Selectable/CategorySelectAble.php @@ -0,0 +1,44 @@ +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); + }); + } + +} diff --git a/app/Helpers/function.php b/app/Helpers/function.php index 2d50253..448c130 100644 --- a/app/Helpers/function.php +++ b/app/Helpers/function.php @@ -1,97 +1,99 @@ -{$return}; - } - - return $category; - } - - return new Category; -} - -/** - * Notes: 获取文章分类详情 - * @Author: 玄尘 - * @Date : 2020/9/10 13:21 - * @param $categoryId - * @param string $result - * @return \App\Models\Article - */ -function getOneArticleBYCate($categoryId, $result = '') -{ - $info = Article::where('category_id', $categoryId)->latest('sort')->latest()->first(); - - if ($info) { - if ($result) { - return $info->{$result}; - } - - return $info; - } else { - return ''; - } - - return new Article; -} - -/** - * Notes: 获取分类下的文章 - * @Author: 玄尘 - * @Date : 2020/9/10 10:05 - * @param $categoryId - * @param $take - * @return \App\Models\Article - */ -function getArticlesBYCate($categoryId, $take = 8, $mark = 'one') -{ - if ($mark == 'one') { - $articles = Article::where('category_id', $categoryId) - ->where('status', 1) - ->latest('sort') - ->latest() - ->take($take) - ->get(); - } else { - $cate = Category::find($categoryId); - $ids = $cate->getAllChildrenId(); - - $articles = Article::whereIn('category_id', $ids) - ->where('status', 1) - ->latest('sort') - ->latest() - ->take($take) - ->get(); - } - - return $articles; -} - -//获取子分类 -function getCateChild($categoryId) -{ - return Category::where('status', 1) - ->where('parent_id', $categoryId) - ->orderBy('order', 'asc') - ->get(); -} - -//获取顶级分类 -function getTopCate($categoryId) -{ - $parent = Category::find($categoryId); - - while ($parent->parent_id != 0) { - $parent = $parent->parent; - } - - return $parent; -} - +{$return}; + } + + return $category; + } + + return new Category; +} + +/** + * Notes: 获取文章分类详情 + * @Author: 玄尘 + * @Date : 2020/9/10 13:21 + * @param $categoryId + * @param string $result + * @return \App\Models\Article + */ +function getOneArticleBYCate($categoryId, $result = '') +{ + $info = Article::where('status', 1) + ->ByCategory($categoryId) + ->latest('sort')->latest()->first(); + + if ($info) { + if ($result) { + return $info->{$result}; + } + + return $info; + } else { + return ''; + } + + return new Article; +} + +/** + * Notes: 获取分类下的文章 + * @Author: 玄尘 + * @Date : 2020/9/10 10:05 + * @param $categoryId + * @param $take + * @return \App\Models\Article + */ +function getArticlesBYCate($categoryId, $take = 8, $mark = 'one') +{ + if ($mark == 'one') { + $articles = Article::where('status', 1) + ->ByCategory($categoryId) + ->latest('sort') + ->latest() + ->take($take) + ->get(); + } else { + $cate = Category::find($categoryId); + $ids = $cate->getAllChildrenId(); + + $articles = Article::where('status', 1) + ->ByCategory($ids) + ->latest('sort') + ->latest() + ->take($take) + ->get(); + } + + return $articles; +} + +//获取子分类 +function getCateChild($categoryId) +{ + return Category::where('status', 1) + ->where('parent_id', $categoryId) + ->orderBy('order', 'asc') + ->get(); +} + +//获取顶级分类 +function getTopCate($categoryId) +{ + $parent = Category::find($categoryId); + + while ($parent->parent_id != 0) { + $parent = $parent->parent; + } + + return $parent; +} + diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index 9ff981a..d22c28e 100644 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -10,18 +10,19 @@ class ArticleController extends Controller /** * 显示分类 - * @param Category $category [description] - * @return [type] [description] + * @param \App\Models\Article $article + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View [type] [description] */ public function show(Article $article) { - $parent = $category = $article->category; - if ($category->childrens->isEmpty()) { - $parent = $category->parent; - } - $advert = Advert::where('category_id',73)->first(); +// $parent = $category = $article->category; + // if ($category->childrens->isEmpty()) { + // $parent = $category->parent; + // } + // $advert = Advert::where('category_id',73)->first(); - return view('article.show', compact('article', 'parent', 'category','advert')); + return view('article.show', compact('article')); +// return view('article.show', compact('article', 'parent', 'category','advert')); } diff --git a/app/Http/Controllers/Auth/ConfirmPasswordController.php b/app/Http/Controllers/Auth/ConfirmPasswordController.php deleted file mode 100644 index 138c1f0..0000000 --- a/app/Http/Controllers/Auth/ConfirmPasswordController.php +++ /dev/null @@ -1,40 +0,0 @@ -middleware('auth'); - } -} diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php deleted file mode 100644 index 465c39c..0000000 --- a/app/Http/Controllers/Auth/ForgotPasswordController.php +++ /dev/null @@ -1,22 +0,0 @@ -middleware('guest')->except('logout'); - } -} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php deleted file mode 100644 index c6a6de6..0000000 --- a/app/Http/Controllers/Auth/RegisterController.php +++ /dev/null @@ -1,73 +0,0 @@ -middleware('guest'); - } - - /** - * Get a validator for an incoming registration request. - * - * @param array $data - * @return \Illuminate\Contracts\Validation\Validator - */ - protected function validator(array $data) - { - return Validator::make($data, [ - 'name' => ['required', 'string', 'max:255'], - 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], - 'password' => ['required', 'string', 'min:8', 'confirmed'], - ]); - } - - /** - * Create a new user instance after a valid registration. - * - * @param array $data - * @return \App\User - */ - protected function create(array $data) - { - return User::create([ - 'name' => $data['name'], - 'email' => $data['email'], - 'password' => Hash::make($data['password']), - ]); - } -} diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php deleted file mode 100644 index b1726a3..0000000 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ /dev/null @@ -1,30 +0,0 @@ -middleware('auth'); - $this->middleware('signed')->only('verify'); - $this->middleware('throttle:6,1')->only('verify', 'resend'); - } -} diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index bdd329a..1751f1c 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -24,8 +24,6 @@ class CategoryController extends Controller $parent = $category->parent; } - $advert = Advert::where('category_id', 73)->first(); - return view('category.show', compact('articles', 'category', 'parent')); } } diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 449427d..065ae51 100644 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -29,7 +29,8 @@ class IndexController extends Controller //通用获取文章 public function getArticle($category_ids, $take = 3) { - return Article::whereIn('category_id', $category_ids) + return Article::where('status', 1) + ->ByCategory($category_ids) ->select('id', 'description', 'title', 'created_at', 'cover', 'content') ->orderBy('created_at', 'desc') ->take($take) diff --git a/app/Http/Controllers/TestController.php b/app/Http/Controllers/TestController.php index 6cd621b..49fbdfa 100644 --- a/app/Http/Controllers/TestController.php +++ b/app/Http/Controllers/TestController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Article; +use App\Models\ArticleCategory; use App\Models\Category; use App\Models\DedeArchive; use App\Models\DedeArctype; @@ -11,6 +12,7 @@ use Illuminate\Support\Facades\DB; class TestController extends Controller { + use Tree; public function index() @@ -18,17 +20,34 @@ 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 checkCategory() { $article = Article::find(913); dd($article->get_one_cover()); dd(); - $lists = Category::where('created_at','2020-06-03 15:57:55')->get(); - $i=1; - foreach ($lists as $info){ - $old = DedeArctype::select('id', 'reid as parent_id', 'typename as title', 'content')->find($info->oldid); - $cate = Category::where('oldid',$old->parent_id)->first(); + $lists = Category::where('created_at', '2020-06-03 15:57:55')->get(); + $i = 1; + foreach ($lists as $info) { + $old = DedeArctype::select('id', 'reid as parent_id', 'typename as title', 'content') + ->find($info->oldid); + $cate = Category::where('oldid', $old->parent_id)->first(); $info->parent_id = $cate->id; $info->save(); $i++; @@ -38,14 +57,17 @@ class TestController extends Controller dump(count($lists)); dd(); $cateids = Category::where('oldid', '>', 0)->pluck('oldid'); - $oldids = DedeArctype::where('ishidden', 0)->pluck('id'); - $diffids = array_diff($oldids->toArray(), $cateids->toArray()); + $oldids = DedeArctype::where('ishidden', 0)->pluck('id'); + $diffids = array_diff($oldids->toArray(), $cateids->toArray()); dump(count($cateids)); dump(count($oldids)); dump($diffids); foreach ($diffids as $diffid) { - $info = DedeArctype::where('id',$diffid)->where('ishidden', 0)->select('id', 'reid as parent_id', 'typename as title', 'content')->first(); + $info = DedeArctype::where('id', $diffid) + ->where('ishidden', 0) + ->select('id', 'reid as parent_id', 'typename as title', 'content') + ->first(); $data = $this->getData($info); Category::create($data); } @@ -87,9 +109,9 @@ class TestController extends Controller { $lists = Article::where('category_id', 0)->get(); foreach ($lists as $list) { - $old = DedeArchive::find($list->oldid); - $cate = Category::where('oldid',$old->typeid)->first(); - $list ->category_id = $cate->id; + $old = DedeArchive::find($list->oldid); + $cate = Category::where('oldid', $old->typeid)->first(); + $list->category_id = $cate->id; $list->save(); } dd(); @@ -100,7 +122,7 @@ class TestController extends Controller dump(count($oldids)); dump($diffids); die(); - $map = [ + $map = [ 'id' => ['in', $diffids], ]; $list = DedeArchive::whereIn('id', $diffids)->get(); @@ -130,7 +152,7 @@ class TestController extends Controller dd('已经导入过数据'); } $categorys = Category::get(); - $error = $success = []; + $error = $success = []; DedeArchive::whereNotNull('litpic')->chunk(200, function ($articles) use ($categorys) { foreach ($articles as $article) { @@ -170,7 +192,9 @@ class TestController extends Controller if ($categorys->count()) { 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); foreach ($list as $key => $value) { @@ -193,6 +217,7 @@ class TestController extends Controller 'content' => $category['content'], 'status' => 1, ]; + return $data; } diff --git a/app/Models/Article.php b/app/Models/Article.php index 29dbde9..d5c926e 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Models\Traits\BelongsToCategory; use App\Models\Traits\HasOneCover; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Article extends Model { @@ -52,4 +53,35 @@ class Article extends Model } + /** + * 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); + }); + } + } diff --git a/app/Models/ArticleCategory.php b/app/Models/ArticleCategory.php new file mode 100644 index 0000000..897b0d0 --- /dev/null +++ b/app/Models/ArticleCategory.php @@ -0,0 +1,11 @@ +hasOne(Article::class)->where('id', $this->article_id); 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); @@ -74,4 +75,9 @@ class Category extends Model return $ids; } + public function articles(): BelongsToMany + { + return $this->belongsToMany(Article::class); + } + } diff --git a/database/migrations/2021_04_01_151442_create_article_categories_table.php b/database/migrations/2021_04_01_151442_create_article_categories_table.php new file mode 100644 index 0000000..002906d --- /dev/null +++ b/database/migrations/2021_04_01_151442_create_article_categories_table.php @@ -0,0 +1,32 @@ +foreignId('article_id'); + $table->foreignId('category_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + Schema::dropIfExists('article_categories'); + } + +} diff --git a/resources/views/article/show.blade.php b/resources/views/article/show.blade.php index 77530f9..cb92436 100644 --- a/resources/views/article/show.blade.php +++ b/resources/views/article/show.blade.php @@ -4,57 +4,57 @@ @section('content')