Skip to content

Commit c3e1fc1

Browse files
committed
Upgraded to Laravel 12
1 parent 5dbebde commit c3e1fc1

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

app/Models/User.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use Illuminate\Support\Str;
910
use Spatie\Permission\Traits\HasRoles;
1011

1112
class User extends Authenticatable
@@ -20,6 +21,7 @@ class User extends Authenticatable
2021
protected $fillable = [
2122
'name',
2223
'email',
24+
'username',
2325
'password',
2426
];
2527

@@ -41,4 +43,60 @@ class User extends Authenticatable
4143
protected $casts = [
4244
'email_verified_at' => 'datetime',
4345
];
46+
47+
public static function boot()
48+
{
49+
parent::boot();
50+
51+
static::saving(function ($model) {
52+
$model->setUsername();
53+
});
54+
}
55+
56+
protected function usernameExists(string $username): bool
57+
{
58+
return self::where('username', $username)->exists();
59+
}
60+
61+
public function setUsername(): void
62+
{
63+
// Early return if username is already set
64+
if ($this->username) {
65+
return;
66+
}
67+
68+
$baseUsername = $this->generateBaseUsername();
69+
$this->username = $this->generateUniqueUsername($baseUsername);
70+
}
71+
72+
private function generateBaseUsername(): string
73+
{
74+
return Str::of($this->name)
75+
->ascii()
76+
->lower()
77+
->replaceMatches('/[\s._-]+/', '') // Replace multiple special characters at once
78+
->trim();
79+
}
80+
81+
private function generateUniqueUsername(string $baseUsername): string
82+
{
83+
$username = $baseUsername;
84+
85+
// If base username is already unique, return it
86+
if (! $this->usernameExists($username)) {
87+
return $username;
88+
}
89+
90+
// Generate a random suffix between 100000 and 999999
91+
$suffix = random_int(100000, 999999);
92+
$username = $baseUsername.$suffix;
93+
94+
// In the unlikely case of collision, increment until unique
95+
while ($this->usernameExists($username)) {
96+
$suffix++;
97+
$username = $baseUsername.$suffix;
98+
}
99+
100+
return $username;
101+
}
44102
}

composer.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
"license": "MIT",
77
"require": {
88
"php": "^8.2",
9-
"balajidharma/laravel-admin-core": "^2.0",
10-
"inertiajs/inertia-laravel": "^1.0",
11-
"laravel/framework": "^11.0",
9+
"balajidharma/laravel-admin-core": "^3.0",
10+
"inertiajs/inertia-laravel": "^2.0",
11+
"laravel/framework": "^12.0",
1212
"laravel/sanctum": "^4.0",
13-
"laravel/tinker": "^2.9",
14-
"tightenco/ziggy": "^2.0"
13+
"laravel/tinker": "^2.10.1",
14+
"tightenco/ziggy": "^2.4"
1515
},
1616
"require-dev": {
1717
"barryvdh/laravel-debugbar": "^3.7",
1818
"fakerphp/faker": "^1.23",
19-
"laravel/breeze": "^2.0",
20-
"laravel/pint": "^1.13",
21-
"laravel/sail": "^1.26",
19+
"laravel/pail": "^1.2.2",
20+
"laravel/pint": "^1.18",
21+
"laravel/sail": "^1.41",
2222
"mockery/mockery": "^1.6",
23-
"nunomaduro/collision": "^8.1",
24-
"phpunit/phpunit": "^10.5",
25-
"spatie/laravel-ignition": "^2.4"
23+
"nunomaduro/collision": "^8.6",
24+
"phpunit/phpunit": "^11.5.3"
2625
},
2726
"autoload": {
2827
"psr-4": {

database/migrations/0001_01_01_000000_create_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up(): void
1515
$table->id();
1616
$table->string('name');
1717
$table->string('email')->unique();
18+
$table->string('username')->unique();
1819
$table->timestamp('email_verified_at')->nullable();
1920
$table->string('password');
2021
$table->rememberToken();

0 commit comments

Comments
 (0)