1. 筛选是否显示的记录
| public function scopeVisible($query) |
| { |
| return $query->where('is_show', true); |
| } |
2. 根据创建时间筛选最近的记录
| public function scopeRecent($query, $days = 7) |
| { |
| return $query->where('created_at', '>=', now()->subDays($days)); |
| } |
3. 根据更新日期排序
| public function scopeOrderByUpdated($query, $direction = 'desc') |
| { |
| return $query->orderBy('updated_at', $direction); |
| } |
4. 根据名称模糊搜索
| public function scopeSearchByName($query, $name) |
| { |
| return $query->where('name', 'like', '%' . $name . '%'); |
| } |
5. 根据父级分类筛选
| public function scopeOfParent($query, $parentId) |
| { |
| return $query->where('parent_id', $parentId); |
| } |
6. 根据多个条件进行筛选
| public function scopeFilter($query, $filters) |
| { |
| return $query->when($filters['topic'] ?? null, function ($query, $topic) { |
| return $query->where('topic', $topic); |
| }) |
| ->when($filters['level'] ?? null, function ($query, $level) { |
| return $query->where('level', array_search($level, self::LEVELS)); |
| }) |
| ->when($filters['is_show'] ?? null, function ($query, $is_show) { |
| return $query->where('is_show', $is_show); |
| }); |
| } |
7. 筛选热门或流行的记录(假设有一个 views
字段)
| public function scopePopular($query, $threshold = 100) |
| { |
| return $query->where('views', '>=', $threshold); |
| } |
8. 获取子级分类
| public function scopeWithChildren($query) |
| { |
| return $query->with('children'); |
| } |
定义作用域
| namespace App\Models; |
| |
| use Illuminate\Database\Eloquent\Factories\HasFactory; |
| use Illuminate\Database\Eloquent\Model; |
| |
| class Topic extends Model |
| { |
| use HasFactory; |
| |
| protected $fillable = [ |
| 'name', |
| 'parent_id', |
| 'description', |
| 'order', |
| 'is_show', |
| 'topic', |
| 'level', |
| 'sequence', |
| ]; |
| |
| const LEVELS = [ |
| 1 => 'beginner', |
| 2 => 'intermediate', |
| 3 => 'advanced', |
| ]; |
| |
| public function getLevelAttribute($value) |
| { |
| return self::LEVELS[$value] ?? null; |
| } |
| |
| public function setLevelAttribute($value) |
| { |
| $this->attributes['level'] = array_search($value, self::LEVELS); |
| } |
| |
| |
| public function parent() |
| { |
| return $this->belongsTo(Topic::class, 'parent_id'); |
| } |
| |
| public function children() |
| { |
| return $this->hasMany(Topic::class, 'parent_id'); |
| } |
| |
| |
| public function scopeVisible($query) |
| { |
| return $query->where('is_show', true); |
| } |
| |
| |
| public function scopeOfType($query, $type) |
| { |
| return $query->where('topic', $type); |
| } |
| |
| |
| public function scopeOfLevel($query, $level) |
| { |
| return $query->where('level', array_search($level, self::LEVELS)); |
| } |
| |
| |
| public function scopeOrdered($query) |
| { |
| return $query->orderBy('sequence'); |
| } |
| |
| |
| public function scopeRecent($query, $days = 7) |
| { |
| return $query->where('created_at', '>=', now()->subDays($days)); |
| } |
| |
| |
| public function scopeOrderByUpdated($query, $direction = 'desc') |
| { |
| return $query->orderBy('updated_at', $direction); |
| } |
| |
| |
| public function scopeSearchByName($query, $name) |
| { |
| return $query->where('name', 'like', '%' . $name . '%'); |
| } |
| |
| |
| public function scopeOfParent($query, $parentId) |
| { |
| return $query->where('parent_id', $parentId); |
| } |
| |
| |
| public function scopeFilter($query, $filters) |
| { |
| return $query->when($filters['topic'] ?? null, function ($query, $topic) { |
| return $query->where('topic', $topic); |
| }) |
| ->when($filters['level'] ?? null, function ($query, $level) { |
| return $query->where('level', array_search($level, self::LEVELS)); |
| }) |
| ->when($filters['is_show'] ?? null, function ($query, $is_show) { |
| return $query->where('is_show', $is_show); |
| }); |
| } |
| |
| |
| public function scopePopular($query, $threshold = 100) |
| { |
| return $query->where('views', '>=', $threshold); |
| } |
| |
| |
| public function scopeWithChildren($query) |
| { |
| return $query->with('children'); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现