Skip to content

Commit cfe23e9

Browse files
authored
Merge pull request #72 from balajidharma/2.x-Changes
2.x changes
2 parents 2a81d64 + b0e53f5 commit cfe23e9

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
[![Laravel VUE admin panel preview](https://user-images.githubusercontent.com/6037466/184547401-1c481008-e013-4ba0-b9a8-3eaf3ff7b9a1.png)](https://github.com/balajidharma/laravel-vue-admin-panel)
1010

1111
## Built with
12-
- [Laravel 11](https://github.com/laravel/framework)
12+
- [Laravel 12](https://github.com/laravel/framework)
1313
- [spatie/laravel-permission](https://github.com/spatie/laravel-permission)
14-
- [Laravel Breeze](https://github.com/laravel/breeze)
1514
- [balajidharma/laravel-menu](https://github.com/balajidharma/laravel-menu)
1615
- [Vue 3](https://vuejs.org/)
1716
- [Tailwind CSS](https://tailwindcss.com/)

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();

resources/js/Pages/Admin/Category/Item/Create.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const form = useForm({
9393
:options="{ enabled: 'Enabled' }"
9494
/>
9595
<FormField
96+
v-if="!categoryType.is_flat"
9697
label="Parent Item"
9798
:class="{ 'text-red-400': form.errors.parent_id }"
9899
>

resources/js/Pages/Admin/Category/Item/Edit.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const form = useForm({
9898
:options="{ enabled: 'Enabled' }"
9999
/>
100100
<FormField
101+
v-if="!categoryType.is_flat"
101102
label="Parent Item"
102103
:class="{ 'text-red-400': form.errors.parent_id }"
103104
>

0 commit comments

Comments
 (0)