我試圖刪除SoftDeletingScope
作爲特定用戶角色的全局範圍。所以應該以某種方式是這樣的:刪除SoftDeletingScope作爲全球範圍
protected static function boot()
{
parent::boot();
if (Auth::check()) {
// CPOs can see deleted users
if (Auth::user()->hasRole('cpo')) {
static::addGlobalScope('user-cpo-deleted', function(Builder $builder) {
// 1
$builder->withoutGlobalScope(Illuminate\Database\Eloquent\SoftDeletingScope::class);
// 2
$builder->withoutGlobalScopes();
// 3
$builder->withTrashed();
// 4
$builder->where('id', '>=', 1);
});
}
}
}
我試圖解決1-3和,以確保該方法被調用可言,4我記錄的SQL查詢,發現4是所謂的,但不是3之前(準確地說,這些方法並沒有刪除users.deleted_at is null
部分)。我分別嘗試了所有這些。
我知道我可以做類似$users = User::withTrashed()->get();
的工作,但是這樣做並不完全安全,因爲我必須找到每個可以查詢用戶的位置,並將其包裝在if語句中。
我知道它聽起來很愚蠢,但是你可以嘗試鏈接諸如'$ builder-> method() - > method()等等的調用嗎? – TheFallen
感謝您的評論,但@devk的解決方案工作;) – Tim