【发布时间】:2021-09-03 18:24:29
【问题描述】:
我一直在看 codecoure 的评论 livewire 教程。一切正常,直到存储评论的表单没有通过。我试过dd,但我什么也没得到。在 slug 后面的 url 中,它说comment=whateverItyped。我一直在故事发布板上使用本教程,但我不知道错误在哪里。我什至复制并粘贴了完全相同的表格和方法,但表格中没有任何内容。
形式:
<div class="min-w-0 flex-1">
<form wire:submit.prevent="postComment">
<div>
<label for="comment" class="sr-only">Comment body</label>
<textarea id="comment" name="comment" rows="3"
class="shadow-sm block w-full focus:ring-blue-500 focus:border-blue-500 border-gray-300 rounded-md @error('newCommentState.body') border-red-500 @enderror"
placeholder="Write something"
wire:model.defer="newCommentState.body">
</textarea>
@error('newCommentState.body')
<p class="mt-2 text-sm text-red-500">{{ $message }}</p>
@enderror
</div>
<div class="mt-3 flex items-center justify-between">
<button type="submit" class="inline-flex items-center justify-center px-4 py-2 border border-transparent font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Comment
</button>
</div>
</form>
</div>
因为我不知道这个错误在哪里。我不知道除了表格我还应该发布什么。有另一种方法可以获取 cmets,并且效果很好。我认为这是发布请求的问题,但我不确定,因为我是 livewire 新手
namespace App\Http\Livewire;
use Livewire\Component;
class Comments extends Component
{
public $model;
public function postComment()
{
$this->validate([
'newCommentState.body' => 'required'
]);
$comment = $this->model->comments()->make($this->newCommentState);
$comment->user()->associate(auth()->user());
$comment->save();
$this->newCommentState = [
'body' => ''
];
$this->goToPage(1);
}
public function render()
{
$comments = $this->model
->comments()
->with('user', 'children.user', 'children.children')
->parent()
->latest()
->get();
return view('livewire.comments', [
'comments' => $comments
]);
}
}
【问题讨论】:
-
我也是 Livewire 的新手,但我在 Livewire 评论类中看不到
newCommentState对象,以及刀片模板中的wire:model.defer="newCommentState.body应该如何访问它。 -
但是我可能不知道那个教程中使用的技术。
-
@Dave 这不是问题所在。我还没有来到那个部分。我刚刚从教程中复制了确切的代码,这是同样的问题
-
如果您在
postComment()函数中的验证之前添加dd('postComment');并按下您的submit按钮,您会看到dd语句吗?
标签: php laravel laravel-livewire