有一个线程,您可以在其中对创建评论功能进行评论,但当我执行更新请求时,它会显示错误:
The subject field is required.
The type field is required.
The thread field is required.
但这与评论无关,而与线程无关。当我检查页面时,链接是这样action="http://127.0.0.1:8000/comment/4"
的,而代码中的代码是这样写的,action="{{ route('comment.update', $comment->id) }}"
但不知何故,路由会通过另一种方式到达,thread.update
但我不明白为什么,因为当我执行 php artisan "route:list" 时,它会显示路径这是
| | DELETE | comment/{comment}
| comment.destroy | App\Http\Controllers\CommentController@destroy
| web |
| | PUT | comment/{comment} | comment.update |
App\Http\Controllers\CommentController@update | web
|
那么为什么它会通过action="{{ route('threadcomment.store', $thread->id) }}"
而不是action="{{ route('comment.update', $comment->id) }}"
网页.php
Route::put('/comment/{comment}','CommentController@update')->name('comment.update');
Route::delete('/comment/{comment}','CommentController@destroy')->name('comment.destroy');
Route::post('/comment/create/{thread}','CommentController@addThreadComment')->name('threadcomment.store');
形式
<div class="comment-list">
@foreach($thread->comments as $comment)
<h4>{{$comment->body}}</h4>
<lead>{{$comment->user->name}}</lead>
<div class="actions">
{{--<a href="{{ route('thread.edit',$thread->id) }}" class="btn btn-info">Edit</a>--}}
<!-- Modal -->
<!-- Trigger/Open The Modal -->
<button class="btn btn-info" id="myBtn{{$comment->id}}">Edit</button>
<!-- The Modal -->
<div id="myModal{{$comment->id}}" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close{{$comment->id}}">×</span>
<form action-="{{ route('comment.update', $comment->id) }}" method="post" role="form">
{{csrf_field()}}
@method('PUT')
<legend>Edit comment</legend>
<div class="form-group">
<input type="text" class="form-control" name="body" id="" value="{{$comment->body}}">
</div>
<input class="btn btn-info" type="submit" value="Edit">
</form>
</div>
</div>
<script>
const modal{{$comment->id}} = document.getElementById('myModal{{$comment->id}}');
const btn{{$comment->id}} = document.getElementById("myBtn{{$comment->id}}");
const span{{$comment->id}} = document.getElementsByClassName("close{{$comment->id}}")[0];
btn{{$comment->id}}.onclick = function() {
modal{{$comment->id}}.style.display = "block";
};
span{{$comment->id}}.onclick = function() {
modal{{$comment->id}}.style.display = "none";
};
window.onclick = function(event) {
if (event.target == modal{{$comment->id}}) {
modal{{$comment->id}}.style.display = "none";
}
}
</script>
<form action="{{ route('comment.destroy', $comment->id) }}" method="post" class="inline-it">
{{csrf_field()}}
{{method_field('DELETE')}}
<input class="btn btn-danger" type="submit" value="delete">
</form>
</div>
@endforeach
</div>
<div class="comment-form">
<form action="{{ route('threadcomment.store', $thread->id) }}" method="post" role="form">
{{csrf_field()}}
<h4>Create Comment</h4>
<div class="form-group">
<input type="text" class="form-control" name="body" id="" placeholder="Input...">
</div>
<button type="submit" class="btn btn-primary">Comment</button>
</form>
</div>
评论控制器
public function addThreadComment(Request $request, Thread $thread)
{
$this->validate($request,[
'body' => 'required|min:5|max:250'
]);
$comment = new Comment();
$comment->body = $request->body;
$comment->user_id = auth()->user()->id;
$thread->comments()->save($comment);
return back()->withMessage('Comment Created!');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Comment $comment
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Comment $comment)
{
$this->validate($request,[
'body' => 'required|min:5|max:250'
]);
$comment->update($request->all());
return back()->withMessage('Updated');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Comment $comment
* @return \Illuminate\Http\Response
*/
public function destroy(Comment $comment)
{
//
}