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');
}
}
标签:Laravel,function,return,level,实例,query,Scope,where,public
From: https://www.cnblogs.com/laraveler/p/18213612