Форма Laravel Не обновляется и говорит, что другая форма laravel 5.8 - PullRequest
0 голосов
/ 13 марта 2019

Существует ветка, в которой вы можете комментировать функции создания комментариев, но когда я делаю запрос на обновление, появляется сообщение об ошибке:

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) }}"

web.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}}">&times;</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)
{
    //
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...