Skip to content

Commit b9d4010

Browse files
committed
✨ ajout d'une commande pour attribuer des roles aux utilisateurs
1 parent b9f4d31 commit b9d4010

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\User;
6+
use Illuminate\Console\Command;
7+
8+
class AssignUserRole extends Command
9+
{
10+
protected $signature = 'lcm:assign-user-role';
11+
12+
protected $description = 'Assign user role to all users without any role.';
13+
14+
public function handle(): void
15+
{
16+
$this->info('Assigning user role to all users...');
17+
18+
foreach (User::withoutRole()->get() as $user) {
19+
$user->assignRole('user');
20+
}
21+
22+
$this->info('All done!');
23+
}
24+
}

app/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public function scopeModerators(Builder $query): Builder
259259
});
260260
}
261261

262+
public function scopeWithoutRole(Builder $query): Builder
263+
{
264+
return $query->whereDoesntHave('roles');
265+
}
266+
262267
public function scopeVerifiedUsers(Builder $query): Builder
263268
{
264269
return $query->whereNotNull('email_verified_at');

0 commit comments

Comments
 (0)