Edit File: UserEdit.php
<?php namespace App\Livewire\Admin\User; use App\Models\Role; use App\Models\History; use Livewire\Component; use Illuminate\Validation\Rule; use Livewire\Attributes\Layout; use App\Models\User as userModel; use Livewire\Attributes\Validate; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use Illuminate\Database\Eloquent\Collection; class UserEdit extends Component { public $user; public $roles; #[Validate("required|string|max:100")] public string $name; public string $username; public string $email; #[Validate("required")] public string $role_id; public string $password; protected function rules() { return [ 'username' => [ 'required', 'string', 'max:100', Rule::unique('users', 'username')->ignore($this->user->id), ], ]; } public function mount($id) { $this->user = UserModel::find($id); $this->name = $this->user->name; $this->username = $this->user->username; $this->email = $this->user->email ? $this->user->email : "-"; $this->role_id = $this->user->role_id; $this->roles = Role::where("id", ">", Auth::user()->role_id)->get(); } public function update() { $this->validate(); $this->user->name = $this->name; $this->user->username = $this->username; if($this->password) { $this->user->password = $this->password; } $this->user->role_id = $this->role_id; if($this->email) { $this->user->email = $this->email; } $this->user->save(); History::create([ 'name' => Auth::user()->name, 'page' => 'User', 'title' => Auth::user()->name . ' Created New User ' . $this->user->username, 'status' => 'info', ]); Session::flash('status', 'success'); Session::flash('message', 'New User Has Been Created'); dump("akhir"); return redirect()->route("admin.user"); } #[Layout("layouts.admin")] public function render() { return view('livewire.admin.user.user-edit'); } }
Back