未通过 laravel 的 livewire 表格
livewire form not going through laravel
我一直在观看来自 codecoure 的评论 livewire 教程。一切正常,直到存储评论的表格没有通过。我试过dd,但我什么也没得到。在 slug 之后的 url 中,它表示 comment=whateverItyped。我一直在故事 posting board 中使用本教程,但我不知道错误在哪里。我什至复制并粘贴了完全相同的表格和方法,但没有任何内容通过表格。
形式:
<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>
因为我不知道这个错误在哪里。我不知道除了表格我还应该 post 什么。还有另一种方法可以获取评论,而且效果很好。我认为这与 post 请求有关,但我不确定,因为我是 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
]);
}
}
您似乎正在使用 $newCommentState
,但从未声明它。
添加新属性public $newCommentState;
。
我也会创建一个这样的方法:
// this will initialize $newCommentState
public function mount() {
$this->newCommentState = [
'body' => ''
];
}
我一直在观看来自 codecoure 的评论 livewire 教程。一切正常,直到存储评论的表格没有通过。我试过dd,但我什么也没得到。在 slug 之后的 url 中,它表示 comment=whateverItyped。我一直在故事 posting board 中使用本教程,但我不知道错误在哪里。我什至复制并粘贴了完全相同的表格和方法,但没有任何内容通过表格。
形式:
<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>
因为我不知道这个错误在哪里。我不知道除了表格我还应该 post 什么。还有另一种方法可以获取评论,而且效果很好。我认为这与 post 请求有关,但我不确定,因为我是 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
]);
}
}
您似乎正在使用 $newCommentState
,但从未声明它。
添加新属性public $newCommentState;
。
我也会创建一个这样的方法:
// this will initialize $newCommentState
public function mount() {
$this->newCommentState = [
'body' => ''
];
}