自动将 where 子句添加到模型上的每个查询

Automatically add where clause to every queries on a model

目前我要Model::where('status', 1)在向其添加任何其他子查询之前过滤掉非活动模型。我发现自己一直在这样做,而且压力很大,因为有时我会忘记添加那部分。我希望有一些东西可以在我 运行 对模型进行查询时自动添加 where 子句。类似于典型的 Laravel SoftDeletes 的东西,它会自动将 WHERE deleted_at IS NULL 子句添加到所有查询。

示例:

不用写Model::where('status', 1)->first(),我可以写Model::first(),这会自动转换为Model::where('status', 1)->first()

请问我该如何实现?

您正在寻找的是 Query Scopes