user profile updates

This commit is contained in:
yuriko 🦊 2025-07-26 15:06:05 -04:00
parent c7c621b086
commit ea140671bc
9 changed files with 78 additions and 27 deletions

View file

@ -4,6 +4,7 @@ namespace App\Livewire\Pages;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;
@ -14,15 +15,39 @@ class Profile extends Component
public User $user;
#[Validate('image|max:10240')]
public $avatar = null;
public string $name = '';
public function mount(User $user)
{
$this->user = $user;
$this->name = $user->name;
}
public function render()
{
return view('livewire.pages.profile')->title($this->user->name);
}
public function updated()
public function rules()
{
return [
'avatar' => [
'nullable',
'image',
'max:10240',
],
'name' => [
'required',
'string',
'min:2',
'max:40',
Rule::unique('users', 'name')->ignore($this->user),
],
];
}
public function updated($name, $value)
{
if (Auth::id() != $this->user->id)
{
@ -31,10 +56,16 @@ class Profile extends Component
$this->validate();
$this->user->avatarExt = $this->avatar->getClientOriginalExtension();
$this->user->save();
$this->user->update([ $name => $value ]);
if (isset($this->avatar))
{
$this->user->avatarExt = $this->avatar->getClientOriginalExtension();
$this->user->save();
$this->avatar->storeAs('avatars', $this->user->id);
return $this->redirect("/profiles/{$this->user->id}");
}
$this->avatar->storeAs('avatars', $this->user->id);
return $this->redirect("/profiles/{$this->user->id}");
}
}