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
1 year ago
|
<?php
|
||
|
|
||
1 year ago
|
namespace App\Http\Controllers\Admin;
|
||
1 year ago
|
|
||
1 year ago
|
use App\Http\Controllers\Controller;
|
||
1 year 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');
|
||
|
}
|
||
|
|
||
1 year 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',
|
||
1 year 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',
|
||
1 year ago
|
]);
|
||
|
|
||
1 year ago
|
$file = $request->file('photo');
|
||
|
$file->store('uploads', 'public');
|
||
|
|
||
1 year 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'),
|
||
1 year 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'])
|
||
|
]);
|
||
|
}
|
||
|
|
||
1 year 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,
|
||
1 year 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',
|
||
1 year ago
|
]);
|
||
|
|
||
|
if ($user->role != null) {
|
||
|
$request->validate([
|
||
1 year ago
|
'role_id' => 'required|ulid|exists:roles,id',
|
||
1 year ago
|
]);
|
||
|
}
|
||
|
|
||
1 year ago
|
if ($request->hasFile('photo')) {
|
||
|
$file = $request->file('photo');
|
||
|
$file->store('uploads', 'public');
|
||
|
$user->fill(['photo' => $file->hashName('uploads')]);
|
||
|
}
|
||
|
|
||
1 year 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,
|
||
1 year 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']);
|
||
|
}
|
||
|
}
|