From 644dfb08961a2d9dbc8e27f1de48ced4c9c2ba2d Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Mon, 1 Jan 2018 15:22:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=BD=9C=E7=94=A8=E5=9F=9F?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/CategoriesController.php | 5 ++-- app/Http/Controllers/TopicsController.php | 5 ++-- app/Models/Category.php | 2 +- app/Models/Topic.php | 29 +++++++++++++++++++ resources/views/topics/index.blade.php | 6 ++-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 2529540..861894c 100644 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -4,12 +4,13 @@ namespace App\Http\Controllers; use App\Models\Category; use App\Models\Topic; +use Illuminate\Http\Request; class CategoriesController extends Controller { - public function show(Category $category) + public function show(Category $category, Request $request) { - $topics = Topic::where('category_id', $category->id)->with('user', 'category')->paginate(); + $topics = Topic::where('category_id', $category->id)->withOrder($request->order)->with('user', 'category')->paginate(); return view('topics.index', compact('topics', 'category')); } } diff --git a/app/Http/Controllers/TopicsController.php b/app/Http/Controllers/TopicsController.php index 6880c76..f462cbf 100644 --- a/app/Http/Controllers/TopicsController.php +++ b/app/Http/Controllers/TopicsController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Http\Requests\TopicRequest; use App\Models\Topic; +use Illuminate\Http\Request; class TopicsController extends Controller { @@ -12,9 +13,9 @@ class TopicsController extends Controller $this->middleware('auth', ['except' => ['index', 'show']]); } - public function index() + public function index(Request $request, Topic $topic) { - $topics = Topic::with('user', 'category')->paginate(); + $topics = $topic->with('user', 'category')->withOrder($request->order)->paginate(); return view('topics.index', compact('topics')); } diff --git a/app/Models/Category.php b/app/Models/Category.php index 07a5f91..d3a4c95 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -2,7 +2,7 @@ namespace App\Models; -class Category extends BaseModel +class Category extends Model { protected $guarded = ['id']; diff --git a/app/Models/Topic.php b/app/Models/Topic.php index f6b20e2..a675a31 100644 --- a/app/Models/Topic.php +++ b/app/Models/Topic.php @@ -7,6 +7,35 @@ class Topic extends Model protected $fillable = ['"title', 'body', 'user_id', 'category_id', 'reply_count', 'view_count', 'last_reply_user_id', 'order', 'excerpt', 'slug']; + public function scopeWithOrder($query, $order) + { + // 不同的排序,使用不同的数据读取逻辑 + switch ($order) { + case 'recent': + $query = $this->recent(); + break; + + default: + $query = $this->recentReplied(); + break; + } + // 预加载防止 N+1 问题 + return $query->with('user', 'category'); + } + + public function scopeRecentReplied($query) + { + // 当话题有新回复时,我们将编写逻辑来更新话题模型的 reply_count 属性, + // 此时会自动触发框架对数据模型 updated_at 时间戳的更新 + return $query->orderBy('updated_at', 'desc'); + } + + public function scopeRecent($query) + { + // 按照创建时间排序 + return $query->orderBy('created_at', 'desc'); + } + public function user() { return $this->belongsTo(User::class); diff --git a/resources/views/topics/index.blade.php b/resources/views/topics/index.blade.php index 747b6b1..fc1ec66 100644 --- a/resources/views/topics/index.blade.php +++ b/resources/views/topics/index.blade.php @@ -16,8 +16,10 @@