journal-intime/app/Http/Controllers/UserController.php

45 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 (isset($validated['avatar'])) {
try {
$user->clearMediaCollection('avatars');
$user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
} catch (DiskDoesNotExist $e) {
} catch (FileDoesNotExist $e) {
} catch (FileIsTooBig $e) {
}
unset($validated['avatar']);
}
if (!array_key_exists('encrypt_messages', $validated)) {
$validated['encrypt_messages'] = 0;
}
$user->update($validated);
return redirect(route('user.index'))->withSuccess('Data saved!');
}
}