Laravel中Scope常用实例

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');
}
// Scope to filter visible topics
public function scopeVisible($query)
{
return $query->where('is_show', true);
}
// Scope to filter by topic type
public function scopeOfType($query, $type)
{
return $query->where('topic', $type);
}
// Scope to filter by level
public function scopeOfLevel($query, $level)
{
return $query->where('level', array_search($level, self::LEVELS));
}
// Scope to order by sequence
public function scopeOrdered($query)
{
return $query->orderBy('sequence');
}
// Scope to filter recent topics
public function scopeRecent($query, $days = 7)
{
return $query->where('created_at', '>=', now()->subDays($days));
}
// Scope to order by updated time
public function scopeOrderByUpdated($query, $direction = 'desc')
{
return $query->orderBy('updated_at', $direction);
}
// Scope to search by name
public function scopeSearchByName($query, $name)
{
return $query->where('name', 'like', '%' . $name . '%');
}
// Scope to filter by parent ID
public function scopeOfParent($query, $parentId)
{
return $query->where('parent_id', $parentId);
}
// Scope to filter by multiple conditions
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);
});
}
// Scope to filter popular topics
public function scopePopular($query, $threshold = 100)
{
return $query->where('views', '>=', $threshold);
}
// Scope to include children
public function scopeWithChildren($query)
{
return $query->with('children');
}
}
posted @   Laravel自学开发  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示