ContactsController.php
4.38 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace App\Http\Controllers;
use App\Contact;
use Inertia\Inertia;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Redirect;
class ContactsController extends Controller
{
public function index()
{
return Inertia::render('Contacts/Index', [
'filters' => Request::all('search', 'trashed'),
'contacts' => Auth::user()->account->contacts()
->with('organization')
->orderByName()
->filter(Request::only('search', 'trashed'))
->paginate()
->transform(function ($contact) {
return [
'id' => $contact->id,
'name' => $contact->name,
'phone' => $contact->phone,
'city' => $contact->city,
'deleted_at' => $contact->deleted_at,
'organization' => $contact->organization ? $contact->organization->only('name') : null,
];
}),
]);
}
public function create()
{
return Inertia::render('Contacts/Create', [
'organizations' => Auth::user()->account
->organizations()
->orderBy('name')
->get()
->map
->only('id', 'name'),
]);
}
public function store()
{
Auth::user()->account->contacts()->create(
Request::validate([
'first_name' => ['required', 'max:50'],
'last_name' => ['required', 'max:50'],
'organization_id' => ['nullable', Rule::exists('organizations', 'id')->where(function ($query) {
$query->where('account_id', Auth::user()->account_id);
})],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
);
return Redirect::route('contacts');
}
public function edit(Contact $contact)
{
return Inertia::render('Contacts/Edit', [
'contact' => [
'id' => $contact->id,
'first_name' => $contact->first_name,
'last_name' => $contact->last_name,
'organization_id' => $contact->organization_id,
'email' => $contact->email,
'phone' => $contact->phone,
'address' => $contact->address,
'city' => $contact->city,
'region' => $contact->region,
'country' => $contact->country,
'postal_code' => $contact->postal_code,
'deleted_at' => $contact->deleted_at,
],
'organizations' => Auth::user()->account->organizations()
->orderBy('name')
->get()
->map
->only('id', 'name'),
]);
}
public function update(Contact $contact)
{
$contact->update(
Request::validate([
'first_name' => ['required', 'max:50'],
'last_name' => ['required', 'max:50'],
'organization_id' => ['nullable', Rule::exists('organizations', 'id')->where(function ($query) {
$query->where('account_id', Auth::user()->account_id);
})],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
);
return Redirect::route('contacts.edit', $contact);
}
public function destroy(Contact $contact)
{
$contact->delete();
return Redirect::route('contacts.edit', $contact);
}
public function restore(Contact $contact)
{
$contact->restore();
return Redirect::route('contacts.edit', $contact);
}
}