diff --git a/app/Http/Traits/UniqueUndeletedTrait.php b/app/Http/Traits/UniqueUndeletedTrait.php new file mode 100644 index 0000000000..63a3740b54 --- /dev/null +++ b/app/Http/Traits/UniqueUndeletedTrait.php @@ -0,0 +1,23 @@ +exists) { + return 'unique_undeleted:'.$this->table.','. $this->getKey(); + } + + return 'unique_undeleted:'.$this->table.',0'; + } +} diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 2909f97aa5..0944a1bb38 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Log; use Parsedown; use Watson\Validating\ValidatingTrait; +use App\Http\Traits\UniqueUndeletedTrait; use DateTime; use App\Models\Setting; use App\Helpers\Helper; @@ -40,6 +41,7 @@ class Asset extends Depreciable */ protected $injectUniqueIdentifier = true; use ValidatingTrait; + use UniqueUndeletedTrait; protected $rules = [ 'name' => 'min:2|max:255', @@ -51,7 +53,7 @@ class Asset extends Depreciable 'checkout_date' => 'date|max:10|min:10', 'checkin_date' => 'date|max:10|min:10', 'supplier_id' => 'integer', - 'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets', + 'asset_tag' => 'required|min:1|max:255|unique_undeleted', 'status' => 'integer', ]; @@ -70,6 +72,8 @@ class Asset extends Depreciable } + + /** * Checkout asset */ diff --git a/app/Models/Category.php b/app/Models/Category.php index d86a155495..8c4cd17f40 100755 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Watson\Validating\ValidatingTrait; +use App\Http\Traits\UniqueUndeletedTrait; /** * Model for Categories. Categories are a higher-level group @@ -38,6 +39,7 @@ class Category extends Model */ protected $injectUniqueIdentifier = true; use ValidatingTrait; + use UniqueUndeletedTrait; /** diff --git a/app/Models/Company.php b/app/Models/Company.php index 17d52b9d7d..ab3aad5dcf 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Watson\Validating\ValidatingTrait; +use App\Http\Traits\UniqueUndeletedTrait; use Auth; use DB; @@ -30,6 +31,7 @@ final class Company extends Model */ protected $injectUniqueIdentifier = true; use ValidatingTrait; + use UniqueUndeletedTrait; /** diff --git a/app/Models/Location.php b/app/Models/Location.php index 43d9d591cb..192ad5a1be 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use App\Models\User; use App\Models\Asset; use Watson\Validating\ValidatingTrait; +use App\Http\Traits\UniqueUndeletedTrait; class Location extends Model { @@ -31,6 +32,7 @@ class Location extends Model */ protected $injectUniqueIdentifier = true; use ValidatingTrait; + use UniqueUndeletedTrait; /** diff --git a/app/Models/Statuslabel.php b/app/Models/Statuslabel.php index cc331a54e7..5f10aa5a09 100755 --- a/app/Models/Statuslabel.php +++ b/app/Models/Statuslabel.php @@ -4,11 +4,13 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Watson\Validating\ValidatingTrait; +use App\Http\Traits\UniqueUndeletedTrait; class Statuslabel extends Model { use SoftDeletes; use ValidatingTrait; + use UniqueUndeletedTrait; protected $injectUniqueIdentifier = true; protected $dates = ['deleted_at']; diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index 254e49b190..fc37aa3ffc 100755 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Watson\Validating\ValidatingTrait; +use App\Http\Traits\UniqueUndeletedTrait; class Supplier extends Model { @@ -35,6 +36,7 @@ class Supplier extends Model */ protected $injectUniqueIdentifier = true; use ValidatingTrait; + use UniqueUndeletedTrait; /** * The attributes that are mass assignable. diff --git a/app/Models/User.php b/app/Models/User.php index ace1351feb..d678b4e51a 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,8 +7,8 @@ use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Watson\Validating\ValidatingTrait; -use App\Models\Company; use Illuminate\Database\Eloquent\SoftDeletes; +use App\Http\Traits\UniqueUndeletedTrait; class User extends Model implements AuthenticatableContract, CanResetPasswordContract { @@ -17,6 +17,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon use ValidatingTrait; use Authenticatable; use CanResetPassword; + use UniqueUndeletedTrait; protected $dates = ['deleted_at']; protected $table = 'users'; diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 1175bc303d..d9bca25292 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -5,6 +5,7 @@ use Validator; use Illuminate\Support\ServiceProvider; use DB; + /** * This service provider handles a few custom validation rules. * @@ -54,13 +55,13 @@ class AppServiceProvider extends ServiceProvider // (I think this is a bug in Laravel's validator?) Validator::extend('unique_undeleted', function($attribute, $value, $parameters, $validator) { - $count = DB::table($parameters[0])->where($attribute,'=',$value)->whereNull('deleted_at')->count(); + $count = DB::table($parameters[0])->select('id')->where($attribute,'=',$value)->whereNull('deleted_at')->where('id','!=',$parameters[1])->count(); if ($count < 1) { return true; + } else { + return false; } - return false; - }); }