0

我正在制作一个评论系统,livewire 在系统的其他部分正常工作,但是当我想编辑评论时,wire:model 没有收到信息,但是如果我在视图上打印变量,它会正确显示。

我展示了组件的代码:

public $editComment;


    public function edit(Comment $comment)
    {
        $this->editComment = $comment;

    }

    public function mount()
    {
         $this->editComment = new Comment();
    }

我显示视图:

   @if($comment->user_id == Auth::user()->id)
      <i title="Editar mensaje" wire:click="edit({{$comment}})" class="fas fa-edit"></i>
      <i wire:click.prevent="confirmDelete({{$comment}})" title="Borrar mensaje" class="far fa-trash-alt"></i>
   @endif
 </div>
    
  @if($comment->id == $editComment->id)
    {{$editComment}} {{-- Here it shows it correctly --}}
        <form wire:submit.prevent="update">
           <input wire:model="editComment" type="text">
4

3 回答 3

0

如果可以打印对象,则缺少的部分是组件中的规则

protected function rules(): array
{
    return [
        'editComment.comment' => [
            'string',
            'required',
        ],
        'editComment.user_id' => [
            'string',
            'required',
        ],
        'editComment.commentable_id' => [
            'string',
            'required',
        ],
        'editComment.commentable_type' => [
            'string',
            'required',
        ],
    ];
}

该函数是根据 Mark 回复中评论中提供的对象结构创建的。您还可以在此处查看文档

于 2022-02-16T19:23:22.850 回答
0

从我的角度来看,您正在编辑方法中传递一个评论对象。您应该传递该特定评论的 id。因为您在编辑方法(评论 $comment)中作为依赖注入访问,例如:

<i title="Editar mensaje" wire:click="edit({{$comment->id}})" class="fas fa-edit"></i>
<i wire:click.prevent="confirmDelete({{$comment->id}})" title="Borrar mensaje" class="far fa-trash-alt"></i>

然后在编辑方法中:

    public function edit(Comment $comment)
    {
        $this->editComment = $comment->body;//or the column that holds the actual text of that comment

    }

第二种解决方案是您可以传递评论的实际正文。并使用没有依赖注入的简单变量访问它。

<i title="Editar mensaje" wire:click="edit({{$comment->body}})" class="fas fa-edit"></i>
    <i wire:click.prevent="confirmDelete({{$comment->id}})" title="Borrar mensaje" class="far fa-trash-alt"></i>

然后在编辑方法中:

    public function edit( $comment)
    {
        $this->editComment = $comment;

    }

对我来说,第二种解决方案更方便。

于 2021-02-11T20:18:06.803 回答
0

我猜这是因为 $editComment 是一个对象。当您使用 {{ Blade }} 语法时,它被转换为字符串,而在 wire:model 中则不是,因此您必须将 wire:model 转换为包含字符串的 Comment 属性。

<input wire:model="comment.string" />

同样,您的编辑方法需要一个注释,但会收到一个字符串,因此您可能需要类似以下内容:

public function edit($comment)
{
  $this->editComment = new Comment($comment);
  // or something similar
}

于 2021-02-11T16:42:35.753 回答