-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateEvent.php
61 lines (55 loc) · 1.53 KB
/
CreateEvent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace App\Console\Commands;
use App\Models\Event;
use App\Models\Role;
use Carbon\Carbon;
use Illuminate\Console\Command;
class CreateEvent extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'registration:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): void
{
$name = $this->ask('What is the name of the event?');
/** @var string $startDateInput */
$startDateInput = $this->ask('When does it start?');
$start = Carbon::parse($startDateInput);
/** @var string $endDateInput */
$endDateInput = $this->ask('When does it end?');
$end = Carbon::parse($endDateInput);
$event = Event::factory()->create([
'name' => $name,
'start' => $start,
'end' => $end,
]);
$this->assignRolesToEvent($event);
}
private function assignRolesToEvent(Event $event): void
{
$roles = $this->choice(
'Who is the target audience of the event?',
Role::all()->map(fn (Role $role) => $role->name)->toArray(),
multiple: true
);
if (is_array($roles)) {
foreach ($roles as $role) {
$event->giveRole($role);
}
} else {
$event->giveRole($roles);
}
}
}