You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.6 KiB
PHTML
123 lines
3.6 KiB
PHTML
2 years ago
|
<?php
|
||
|
|
||
1 year ago
|
namespace App\Http\Controllers\Admin;
|
||
2 years ago
|
|
||
1 year ago
|
use App\Http\Controllers\Controller;
|
||
2 years ago
|
use App\Models\User;
|
||
|
use Illuminate\Http\RedirectResponse;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Inertia\Response;
|
||
|
|
||
|
class UserController extends Controller
|
||
|
{
|
||
|
public function index(Request $request): Response
|
||
|
{
|
||
|
$query = User::query()->with(['role']);
|
||
|
|
||
|
if ($request->q) {
|
||
|
$query->where('name', 'like', "%{$request->q}%");
|
||
|
}
|
||
|
|
||
|
$query->orderBy('created_at', 'desc');
|
||
|
|
||
|
return inertia('User/Index', [
|
||
|
'data' => $query->paginate(10),
|
||
|
]);
|
||
|
}
|
||
|
|
||
1 year ago
|
public function create()
|
||
|
{
|
||
|
return inertia('User/Form');
|
||
|
}
|
||
|
|
||
2 years ago
|
public function store(Request $request): RedirectResponse
|
||
|
{
|
||
|
$request->validate([
|
||
|
'name' => 'required|string|max:255',
|
||
|
'email' => 'required|email|unique:users,email',
|
||
|
'password' => 'required|string|max:255',
|
||
2 years ago
|
'role_id' => 'required|ulid|exists:roles,id',
|
||
1 year ago
|
'username' => 'required|alpha_dash|unique:users,username',
|
||
|
'phone_wa' => 'required|string',
|
||
|
'photo' => 'required|image',
|
||
2 years ago
|
]);
|
||
|
|
||
1 year ago
|
$file = $request->file('photo');
|
||
|
$file->store('uploads', 'public');
|
||
|
|
||
2 years ago
|
User::create([
|
||
|
'name' => $request->name,
|
||
|
'email' => $request->email,
|
||
|
'password' => bcrypt($request->password),
|
||
|
'role_id' => $request->role_id,
|
||
1 year ago
|
'username' => $request->username,
|
||
|
'phone_wa' => $request->phone_wa,
|
||
|
'photo' => $file->hashName('uploads'),
|
||
2 years ago
|
]);
|
||
|
|
||
|
return redirect()->route('user.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed saved']);
|
||
|
}
|
||
|
|
||
1 year ago
|
public function edit(User $user)
|
||
|
{
|
||
|
return inertia('User/Form', [
|
||
|
'user' => $user->load(['role'])
|
||
|
]);
|
||
|
}
|
||
|
|
||
2 years ago
|
public function update(Request $request, User $user): RedirectResponse
|
||
|
{
|
||
|
$request->validate([
|
||
|
'name' => 'required|string|max:255',
|
||
1 year ago
|
'email' => 'required|email|unique:users,email,' . $user->id,
|
||
2 years ago
|
'password' => 'nullable|string|max:255',
|
||
1 year ago
|
'username' => 'required|alpha_dash|unique:users,username,' . $user->id,
|
||
|
'phone_wa' => 'required|string',
|
||
|
'photo' => 'nullable|image',
|
||
2 years ago
|
]);
|
||
|
|
||
|
if ($user->role != null) {
|
||
|
$request->validate([
|
||
2 years ago
|
'role_id' => 'required|ulid|exists:roles,id',
|
||
2 years ago
|
]);
|
||
|
}
|
||
|
|
||
1 year ago
|
if ($request->hasFile('photo')) {
|
||
|
$file = $request->file('photo');
|
||
|
$file->store('uploads', 'public');
|
||
|
$user->fill(['photo' => $file->hashName('uploads')]);
|
||
|
}
|
||
|
|
||
2 years ago
|
$user->fill([
|
||
|
'email' => $request->email,
|
||
|
'name' => $request->name,
|
||
|
'role_id' => $request->role_id,
|
||
1 year ago
|
'username' => $request->username,
|
||
|
'phone_wa' => $request->phone_wa,
|
||
2 years ago
|
]);
|
||
|
|
||
|
if ($request->password != '') {
|
||
|
$user->password = bcrypt($request->password);
|
||
|
}
|
||
|
|
||
|
$user->save();
|
||
|
|
||
|
return redirect()->route('user.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed updated']);
|
||
|
}
|
||
|
|
||
|
public function destroy(User $user): RedirectResponse
|
||
|
{
|
||
|
if ($user->role_id == null) {
|
||
|
return redirect()->route('user.index')
|
||
|
->with('message', ['type' => 'error', 'message' => 'Item default can\'t deleted']);
|
||
|
}
|
||
|
|
||
|
$user->delete();
|
||
|
|
||
|
return redirect()->route('user.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed deleted']);
|
||
|
}
|
||
|
}
|