提问者:小点点

配置文件未在Laravel Livewire中更新


我要创建一个用户配置文件组件,这将允许用户更新那里的帐户,所以我已经创建了配置文件更新形式,当我输入新的细节,我得到没有user_id

public $users, $name, $email, $user_id, $creator_name, $creator_bio;
    public $updateMode = false;

    private function resetInputFields(){
        $this->name = '';
        $this->email = '';
        $this->creator_name = '';
        $this->creator_bio = '';
    }


    public function edit($id)
    {
        $this->updateMode = true;
        $user = User::where('id',$id)->first();
        $this->user_id = $id;
        $this->name = $user->name;
        $this->email = $user->email;
        $this->creator_name = $user->creator_name;
        $this->creator_bio = $user->creator_bio;

    }

    public function cancel()
    {
        $this->updateMode = false;
        $this->resetInputFields();


    }

    public function update()
    {
        $validatedDate = $this->validate([
            'name' => 'required',
            'email' => 'required|email',
        ]);

        if ($this->user_id) {
            dd('There is a user id');
            $user = User::find($this->user_id);
            $user->update([
                'name' => $this->name,
                'email' => $this->email,
            ]);
            $this->updateMode = false;
            session()->flash('message', 'Users Updated Successfully.');
            $this->resetInputFields();

        } else {
             dd('There is NOT a user id');
        }
    }
    // public function delete($id)
    // {
    //     if($id){
    //         User::where('id',$id)->delete();
    //         session()->flash('message', 'Users Deleted Successfully.');
    //     }
    // }
    public function render()
    {
        return view('livewire.profile-update');
    }

这是我的个人资料更新表单,当我单击“更新”时,什么都没有发生,我正在正确地传递livewire脚本

<div>
<form>
    <div class="form-group">
        <input type="hidden" wire:model="user_id">
        <label for="exampleFormControlInput1">Name</label>
        <input type="text" class="form-control" wire:model="name" id="exampleFormControlInput1" placeholder="Enter Name">
        @error('name') <span class="text-danger">{{ $message }}</span>@enderror
    </div>
    <div class="form-group">
        <input type="hidden" wire:model="user_id">
        <label for="exampleFormControlInput1">Creator Name</label>
        <input type="text" class="form-control" wire:model="creator_name" id="exampleFormControlInput1" placeholder="Enter Name">
        @error('creator_name') <span class="text-danger">{{ $message }}</span>@enderror
    </div>
    <div class="form-group">
        <input type="hidden" wire:model="user_id">
        <label for="exampleFormControlInput1">Creator Bio</label>
        <input type="text" class="form-control" wire:model="creator_bio" id="exampleFormControlInput1" placeholder="Enter Name">
        @error('creator_bio') <span class="text-danger">{{ $message }}</span>@enderror
    </div>
    <div class="form-group">
        <label for="exampleFormControlInput2">Email address</label>
        <input type="email" class="form-control" wire:model="email" id="exampleFormControlInput2" placeholder="Enter Email">
        @error('email') <span class="text-danger">{{ $message }}</span>@enderror
    </div>
    <button wire:click.prevent="update()" class="btn btn-dark">Update</button>
    <button wire:click.prevent="cancel()" class="btn btn-danger">Cancel</button>
</form>
</div>

共1个答案

匿名用户

必须在用户类中实现可填充或受保护的属性

protected $fillable = [
   'name','email'
];

// or

protected $guarded = [];