本地作用域排序相关
This commit is contained in:
parent
85291df5b3
commit
644dfb0896
@ -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'));
|
||||
}
|
||||
}
|
||||
|
@ -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'));
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class Category extends BaseModel
|
||||
class Category extends Model
|
||||
{
|
||||
protected $guarded = ['id'];
|
||||
|
||||
|
@ -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);
|
||||
|
@ -16,8 +16,10 @@
|
||||
|
||||
<div class="panel-heading">
|
||||
<ul class="nav nav-pills">
|
||||
<li role="presentation" class="active"><a href="#">最后回复</a></li>
|
||||
<li role="presentation"><a href="#">最新发布</a></li>
|
||||
<li role="presentation" class="{{active_class(!if_query('order','recent'))}}"><a
|
||||
href="{{Request::url()}}?order=default">最后回复</a></li>
|
||||
<li role="presentation" class="{{active_class(if_query('order','recent'))}}"><a
|
||||
href="{{Request::url()}}?order=recent">最新发布</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user