diff --git a/src/HelpersServiceProvider.php b/src/HelpersServiceProvider.php index 7fd7559d..b92cfd55 100755 --- a/src/HelpersServiceProvider.php +++ b/src/HelpersServiceProvider.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Validator; use Maatwebsite\Excel\ExcelServiceProvider; use RonasIT\Support\Middleware\SecurityMiddleware; +use Illuminate\Support\Arr; class HelpersServiceProvider extends ServiceProvider { @@ -18,11 +19,17 @@ public function boot() $router->prependMiddlewareToGroup('web', SecurityMiddleware::class); $router->prependMiddlewareToGroup('api', SecurityMiddleware::class); - Validator::extend('unique_except_of_authorized_user', function ($attribute, $value) { + validator::extend('unique_except_of_authorized_user', function ($attribute, $value, $parameters = []) { + $table = Arr::get($parameters, 0, 'users'); + $keyField = Arr::get($parameters, 1, 'id'); $userId = app(JWTAuth::class)->toUser()->id; - $result = DB::select("select count(*) as entities_count from users where id <> {$userId} and {$attribute} = '{$value}';"); - return $result[0]->entities_count == 0; + $result = DB::table($table) + ->where($keyField, '<>', $userId) + ->whereIn($attribute, Arr::flatten((array) $value)) + ->exists(); + + return !$result; }); app(ExcelServiceProvider::class, ['app' => app()])->boot();