我个人会使用查询范围来获取未删除的记录
public function scopeNotDeleted(Builder $query): Builder
{
return $query->where('is_deleted', 0);
}
然后在获取记录时使用它
User::notDeleted()->where(function(Builder $query) {
$query->where('email', 'test@test.com')->orWhere('email', 'test2@test2.com');
})->get();
如果你正在使用
applyAllLogicFromCurrentBuilder
方法,您还可以将其提取到查询范围,并将其与调用链接起来,如下所示:
User::allLogicFromCurrentBuilder()->notDeleted()->where(function(Builder $query) {
$query->where('email', 'test@test.com')->orWhere('email', 'test2@test2.com');
})->get();
你也可以
applyNotDeleted
get()
方法-这样,如果需要,您可以向它附加任何进一步的语句。我可能会把它转换成
public static
User::applyNotDeleted(User::where(function(Builder $query) use ($email) {
$query->where('email', 'test@test.com')->orWhere('email', 'test2@test2.com');
}))->get();
就我个人而言,我更喜欢范围方法,因为它看起来有点干净。