-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUserResource.stub
91 lines (79 loc) · 2.31 KB
/
UserResource.stub
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace {{ namespace }}Root\Resources;
use Cone\Root\Actions\SendPasswordResetNotification;
use Cone\Root\Actions\SendVerificationNotification;
use Cone\Root\Fields\Date;
use Cone\Root\Fields\Email;
use Cone\Root\Fields\ID;
use Cone\Root\Fields\Text;
use Cone\Root\Models\User;
use Cone\Root\Resources\Resource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class UserResource extends Resource
{
/**
* The model class.
*/
protected string $model = User::class;
/**
* The icon for the resource.
*/
protected string $icon = 'users';
/**
* Get the model for the resource.
*/
public function getModel(): string
{
return $this->model::getProxiedClass();
}
/**
* Define the fields.
*/
public function fields(Request $request): array
{
return [
ID::make()->searchable(),
Text::make(__('Name'), 'name')
->rules(['required', 'string', 'max:256'])
->searchable()
->sortable(),
Email::make(__('Email'), 'email')
->searchable()
->sortable()
->rules(['required', 'string', 'email', 'max:256'])
->createRules(['unique:users'])
->updateRules(static function (Request $request, Model $model): array {
return [Rule::unique('users')->ignoreModel($model)];
}),
Date::make(__('Created At'), 'created_at')
->sortable()
->withTime(),
Date::make(__('Email Verified At'), 'email_verified_at')
->sortable()
->withTime(),
];
}
/**
* Define the actions.
*/
public function actions(Request $request): array
{
return [
new SendVerificationNotification(),
new SendPasswordResetNotification(),
];
}
/**
* Handle the saving form event.
*/
public function saving(Request $request, Model $model): void
{
if (! $model->exists && is_null($model->getAttribute('password'))) {
$model->setAttribute('password', Hash::make(Str::password()));
}
}
}