netgescon-master/app/Http/Controllers/Admin/UserController.php
2025-07-20 14:57:25 +00:00

110 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$users = User::latest()->paginate(15);
return view('admin.users.index', compact('users'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('admin.users.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
'telefono' => 'nullable|string|max:20',
'codice_fiscale' => 'nullable|string|max:16',
'indirizzo' => 'nullable|string|max:255',
'citta' => 'nullable|string|max:100',
'cap' => 'nullable|string|max:10',
'provincia' => 'nullable|string|max:2'
]);
$validated['password'] = Hash::make($validated['password']);
User::create($validated);
return redirect()->route('admin.users.index')
->with('success', 'Utente creato con successo.');
}
/**
* Display the specified resource.
*/
public function show(User $user)
{
return view('admin.users.show', compact('user'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(User $user)
{
return view('admin.users.edit', compact('user'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, User $user)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $user->id,
'password' => 'nullable|string|min:8|confirmed',
'telefono' => 'nullable|string|max:20',
'codice_fiscale' => 'nullable|string|max:16',
'indirizzo' => 'nullable|string|max:255',
'citta' => 'nullable|string|max:100',
'cap' => 'nullable|string|max:10',
'provincia' => 'nullable|string|max:2'
]);
if ($validated['password']) {
$validated['password'] = Hash::make($validated['password']);
} else {
unset($validated['password']);
}
$user->update($validated);
return redirect()->route('admin.users.index')
->with('success', 'Utente aggiornato con successo.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(User $user)
{
$user->delete();
return redirect()->route('admin.users.index')
->with('success', 'Utente eliminato con successo.');
}
}