journal-intime/app/Http/Controllers/UserController.php
Clement Desmidt f97c6a56ec Permet la mise à jour du profil
La mise à jour de l'avatar fonctionne à moitié
Pour #12
2020-03-17 17:33:58 +01:00

42 lines
1.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\UpdateUser;
use App\User;
use Illuminate\Support\Facades\Auth;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig;
class UserController extends Controller
{
public function index()
{
return view('user.index', ['user' => Auth::user()]);
}
public function update(UpdateUser $request)
{
/** @var User $user */
$user_id = Auth::user()->getAuthIdentifier();
$user = User::find($user_id);
$validated = $request->validated();
if (!array_key_exists('encrypt_messages', $validated)) {
$validated['encrypt_messages'] = 0;
}
if (isset($validated['avatar'])) {
try {
$user->clearMediaCollection('avatars');
$user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
} catch (DiskDoesNotExist $e) {
} catch (FileDoesNotExist $e) {
} catch (FileIsTooBig $e) {
}
}
$user->update($validated);
return redirect(route('user.index'))->withSuccess('Data saved!');
}
}