Mager updated
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Models\Person;
|
||||
|
||||
use App\Traits\Uuid;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@@ -52,6 +53,10 @@ protected static function booted(){
|
||||
if(!isset($person->user_id)){
|
||||
$person->user_id = auth()->id();
|
||||
}
|
||||
// Ensure a unique 6-character alphanumeric 'nu' is set globally on create
|
||||
if (empty($person->nu)) {
|
||||
$person->nu = static::generateUniqueNu();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -88,6 +93,13 @@ public function addresses(): HasMany
|
||||
->orderBy('id');
|
||||
}
|
||||
|
||||
public function emails(): HasMany
|
||||
{
|
||||
return $this->hasMany(\App\Models\Email::class, 'person_id')
|
||||
->where('is_active', '=', 1)
|
||||
->orderBy('id');
|
||||
}
|
||||
|
||||
public function group(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Person\PersonGroup::class, 'group_id');
|
||||
@@ -108,4 +120,14 @@ public function clientCase(): HasOne
|
||||
return $this->hasOne(\App\Models\ClientCase::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique 6-character alphanumeric identifier for 'nu'.
|
||||
*/
|
||||
protected static function generateUniqueNu(): string
|
||||
{
|
||||
do {
|
||||
$nu = Str::random(6); // [A-Za-z0-9]
|
||||
} while (static::where('nu', $nu)->exists());
|
||||
return $nu;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user