文章分类由1变多
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;
|
||||
@@ -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');
|
||||
|
||||
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', '分类名称');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,7 +40,8 @@ function getOneCategory($categoryId, $return = '')
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
->where('category_id', $category->id)
|
||||
->ByCategory($article->categories()->pluck('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'));
|
||||
}
|
||||
|
||||
//搜索
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Foundation\Auth\ConfirmsPasswords;
|
||||
|
||||
class ConfirmPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Confirm Password Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password confirmations and
|
||||
| uses a simple trait to include the behavior. You're free to explore
|
||||
| this trait and override any functions that require customization.
|
||||
|
|
||||
*/
|
||||
|
||||
use ConfirmsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users when the intended url fails.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = RouteServiceProvider::HOME;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
|
||||
|
||||
class ForgotPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset emails and
|
||||
| includes a trait which assists in sending these notifications from
|
||||
| your application to your users. Feel free to explore this trait.
|
||||
|
|
||||
*/
|
||||
|
||||
use SendsPasswordResetEmails;
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Login Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller handles authenticating users for the application and
|
||||
| redirecting them to your home screen. The controller uses a trait
|
||||
| to conveniently provide its functionality to your applications.
|
||||
|
|
||||
*/
|
||||
|
||||
use AuthenticatesUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after login.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = RouteServiceProvider::HOME;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest')->except('logout');
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use App\User;
|
||||
use Illuminate\Foundation\Auth\RegistersUsers;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Register Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller handles the registration of new users as well as their
|
||||
| validation and creation. By default this controller uses a trait to
|
||||
| provide this functionality without requiring any additional code.
|
||||
|
|
||||
*/
|
||||
|
||||
use RegistersUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after registration.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = RouteServiceProvider::HOME;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->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']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||||
|
||||
class ResetPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset requests
|
||||
| and uses a simple trait to include this behavior. You're free to
|
||||
| explore this trait and override any methods you wish to tweak.
|
||||
|
|
||||
*/
|
||||
|
||||
use ResetsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users after resetting their password.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = RouteServiceProvider::HOME;
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Foundation\Auth\VerifiesEmails;
|
||||
|
||||
class VerificationController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Email Verification Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling email verification for any
|
||||
| user that recently registered with the application. Emails may also
|
||||
| be re-sent if the user didn't receive the original email message.
|
||||
|
|
||||
*/
|
||||
|
||||
use VerifiesEmails;
|
||||
|
||||
/**
|
||||
* Where to redirect users after verification.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = RouteServiceProvider::HOME;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
$this->middleware('signed')->only('verify');
|
||||
$this->middleware('throttle:6,1')->only('verify', 'resend');
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
|
||||
//
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use App\Models\Traits\HasOneCover;
|
||||
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;
|
||||
|
||||
class Category extends Model
|
||||
@@ -44,7 +45,7 @@ class Category extends Model
|
||||
return $this->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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,9 +8,9 @@
|
||||
<div class="container details">
|
||||
<!-- 文章标题 -->
|
||||
<h3 class="details-title">{{ $article->title }}</h3>
|
||||
@if (in_array($parent->id,config('app.show_time')))
|
||||
{{-- @if (in_array($parent->id,config('app.show_time')))--}}
|
||||
<p class="details-time">{{ $article->created_at }}</p>
|
||||
@endif
|
||||
{{-- @endif--}}
|
||||
<!-- 文章详情 -->
|
||||
<div class="details-content">
|
||||
{{-- <img src="{{ $article->cover_path }}">--}}
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user