From 977dd11c7d5d6f5139ebb429073191e22225396c Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Fri, 2 Apr 2021 09:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=88=86=E7=B1=BB=E7=94=B11?= =?UTF-8?q?=E5=8F=98=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Article/IndexController.php | 28 ++++--- app/Admin/Selectable/CategorySelectAble.php | 37 ++++++++++ app/Helpers/function.php | 14 ++-- app/Http/Controllers/ArticleController.php | 15 ++-- .../Auth/ConfirmPasswordController.php | 40 ---------- .../Auth/ForgotPasswordController.php | 22 ------ app/Http/Controllers/Auth/LoginController.php | 40 ---------- .../Controllers/Auth/RegisterController.php | 73 ------------------- .../Auth/ResetPasswordController.php | 30 -------- .../Auth/VerificationController.php | 42 ----------- app/Http/Controllers/CategoryController.php | 4 +- app/Http/Controllers/IndexController.php | 2 +- app/Http/Controllers/TestController.php | 19 +++++ app/Models/Article.php | 36 ++++++++- app/Models/ArticleCategory.php | 11 +++ app/Models/Category.php | 8 +- ...151442_create_article_categories_table.php | 32 ++++++++ resources/views/articles/show.blade.php | 6 +- routes/web.php | 5 +- 19 files changed, 177 insertions(+), 287 deletions(-) create mode 100644 app/Admin/Selectable/CategorySelectAble.php delete mode 100644 app/Http/Controllers/Auth/ConfirmPasswordController.php delete mode 100644 app/Http/Controllers/Auth/ForgotPasswordController.php delete mode 100644 app/Http/Controllers/Auth/LoginController.php delete mode 100644 app/Http/Controllers/Auth/RegisterController.php delete mode 100644 app/Http/Controllers/Auth/ResetPasswordController.php delete mode 100644 app/Http/Controllers/Auth/VerificationController.php create mode 100644 app/Models/ArticleCategory.php create mode 100644 database/migrations/2021_04_01_151442_create_article_categories_table.php diff --git a/app/Admin/Controllers/Article/IndexController.php b/app/Admin/Controllers/Article/IndexController.php index b69dff5..0a458f6 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; @@ -52,18 +53,21 @@ 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->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]); - }, '选择分类')) - ->when('in', [3, 29], function (Form $form) { - $form->text('working', '工龄'); - $form->text('job', '岗位'); - }) - ->rules('required|min:1', [ - 'required' => '必须选择所属分类', - 'min' => '必须选择所属分类', - ]); + + $form->belongsToMany('categories', CategorySelectAble::class, __('关联分类')); + +// $form->select('category_id', '所属分类') + // ->options(Category::selectOptions(function ($model) { + // return $model->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]); + // }, '选择分类')) + // ->when('in', [3, 29], function (Form $form) { + // $form->text('working', '工龄'); + // $form->text('job', '岗位'); + // }) + // ->rules('required|min:1', [ + // 'required' => '必须选择所属分类', + // 'min' => '必须选择所属分类', + // ]); // $form->text('keywords', '关键词')->rules('nullable'); $form->textarea('description', '内容简介')->rules('max:350'); diff --git a/app/Admin/Selectable/CategorySelectAble.php b/app/Admin/Selectable/CategorySelectAble.php new file mode 100644 index 0000000..bf24684 --- /dev/null +++ b/app/Admin/Selectable/CategorySelectAble.php @@ -0,0 +1,37 @@ +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', '分类名称'); + }); + } + +} diff --git a/app/Helpers/function.php b/app/Helpers/function.php index 5b8551f..4342b7e 100644 --- a/app/Helpers/function.php +++ b/app/Helpers/function.php @@ -13,7 +13,7 @@ use Illuminate\Support\Arr; * @Author: 玄尘 * @Date : 2021/2/3 16:36 * @param $categoryId - * @param string $return + * @param string $return * @return \App\Models\Category */ function getOneCategory($categoryId, $return = '') @@ -35,12 +35,13 @@ function getOneCategory($categoryId, $return = '') * @Author: 玄尘 * @Date : 2020/9/10 13:21 * @param $categoryId - * @param string $result + * @param string $result * @return \App\Models\Article */ function getOneArticleBYCate($categoryId, $result = '') { - $info = Article::where('category_id', $categoryId) + $info = Article::where('status', 1) + ->ByCategory($categoryId) ->latest('sort') ->latest() ->first(); @@ -68,8 +69,8 @@ function getOneArticleBYCate($categoryId, $result = '') */ function getArticlesBYCate($categoryId, $take) { - $articles = Article::where('category_id', $categoryId) - ->where('status', 1) + $articles = Article::where('status', 1) + ->ByCategory($categoryId) ->latest('sort') ->latest() ->take($take) @@ -186,8 +187,9 @@ function getArtilesByCates($cate_id, $take = 8) ->pluck('id') ->toArray(); - return Article::whereIn('category_id', $cate_ids) + return Article::where('status', 1) ->latest() + ->ByCategory($cate_ids) ->take($take) ->get(); } diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index 775b37c..394e5fb 100644 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -12,7 +12,7 @@ class ArticleController extends Controller //文章列表 public function index(Category $category) { - $articles = Article::where('category_id', $category->id) + $articles = Article::ByCategory($category->id) ->orderBy('created_at', 'desc') ->paginate(5); @@ -26,15 +26,12 @@ class ArticleController extends Controller return redirect($article->url); } - $category = $article->category; + $next = Article::where('id', '>', $article->id) + ->ByCategory($article->categories()->pluck('id')) + ->where('status', 1) + ->first(); - $next = Article::where('id', '>', $article->id) - ->where('category_id', $category->id) - ->where('status', 1) - ->first(); - $parent = getTopCate($category->id); - - return view('articles.show', compact('article', 'category', 'next', 'parent')); + return view('articles.show', compact('article', 'next')); } //搜索 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 0221a98..c5511bf 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -45,8 +45,8 @@ class CategoryController extends Controller ->pluck('id') ->toArray(); - $articles = Article::whereIn('category_id', $cate_ids) - ->where('status', 1) + $articles = Article::where('status', 1) + ->ByCategory($cate_ids) ->latest('sort') ->latest() ->paginate($take); diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index eba1cfb..02d182e 100644 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -19,7 +19,7 @@ class IndexController extends Controller public function index() { $data = [ - 'ysxw' => Article::where('category_id', 15)->latest('sort')->latest()->take(8)->get(), + 'ysxw' => Article::ByCategory(15)->latest('sort')->latest()->take(8)->get(), // 'kjcg' => Article::where('category_id', 19)->latest('sort')->latest()->take(8)->get(), // 'lwzl' => Patent::latest('sort')->latest()->take(11)->get(), 'center_advert' => Advert::latest('sort')->latest()->where('category_id', 23)->take(3)->get(), diff --git a/app/Http/Controllers/TestController.php b/app/Http/Controllers/TestController.php index c2eb718..28a473a 100644 --- a/app/Http/Controllers/TestController.php +++ b/app/Http/Controllers/TestController.php @@ -2,6 +2,9 @@ namespace App\Http\Controllers; +use App\Models\Article; +use App\Models\ArticleCategory; + class TestController extends Controller { @@ -10,4 +13,20 @@ 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; + + } + } diff --git a/app/Models/Article.php b/app/Models/Article.php index 02687a9..2481957 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -4,11 +4,12 @@ namespace App\Models; use App\Models\Traits\BelongsToCategory; use App\Models\Traits\HasOneCover; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Article extends Model { - use HasOneCover, BelongsToCategory; + use HasOneCover; const POSITION_A = 1; const POSITION_B = 2; @@ -20,9 +21,40 @@ class Article extends Model self::POSITION_C => '科技成果', ]; - public function getLinkAttribute() + public function getLinkAttribute(): string { 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); + }); + } + } 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, 'id', '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); @@ -64,4 +65,9 @@ class Category extends Model return $this->hasMany(__CLASS__, 'parent_id'); } + 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/articles/show.blade.php b/resources/views/articles/show.blade.php index f7cf146..1814f88 100644 --- a/resources/views/articles/show.blade.php +++ b/resources/views/articles/show.blade.php @@ -8,9 +8,9 @@

{{ $article->title }}

- @if (in_array($parent->id,config('app.show_time'))) -

{{ $article->created_at }}

- @endif + {{-- @if (in_array($parent->id,config('app.show_time')))--}} +

{{ $article->created_at }}

+ {{-- @endif--}}
{{-- --}} diff --git a/routes/web.php b/routes/web.php index 5e2d044..5f57138 100644 --- a/routes/web.php +++ b/routes/web.php @@ -22,7 +22,4 @@ Route::get('patents/{patent}/show', 'PatentController@show')->name('patents.show Route::get('patents/list', 'PatentController@list')->name('patents.list'); //以下为导入数据 -Route::get('test/set_category', 'TestController@set_category'); -Route::get('test/set_article', 'TestController@set_article'); -Route::get('test/set_cate_article', 'TestController@setCateArticle'); -Route::get('test/check_article', 'TestController@checkArticle'); +Route::get('test/set_article_category', 'TestController@set_article_category');