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 @@