Laravel使用Ajax自动更新div内容

时间:2019-07-11 09:43:41

标签: php laravel

我在laravel项目中更新div内容时出现错误。我使用setInterval方法。但是,当我将路线放入load参数时,会引发错误。有人知道我为什么会收到此错误吗?

shownews.blade.php

   <h4 class="comments-title" > <span class="fas fa-comment-alt"></span>
                    {{$news->comments()->count()}}
                    Comments</h4>
                  <div class="row" >
                      <div class="col-md-12 col-md-offset-2" style="overflow-y: scroll; height: 400px;
                      width: 400px; " id="commentarea" >

                          @foreach($news->comments as $comment)
                            <div class="comment" style="background-color: #f6efef;" >
                          <div class="author-info">
                              <img src={{"https://www.gravatar.com/avatar/" . md5(strtolower(trim($comment->email))) . "?s=50&d=retro" }} class="author-image" id="image">

                              <div class="author-name">
                                   <h4>{{$comment->name}} </h4>
                                   <p class="author-time"> {{  date('jS F, Y - g:iA' ,strtotime($comment->created_at)) }}</p>
                              </div>
                          </div>
                            <div class="comment-content">
                                    {{$comment->comment}}
                            </div>
                            </div>
                          @endforeach
                      </div>
                  </div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script>
            $(document).ready(function() {
             setInterval(function() {
              $('#commentarea').load('{{ action('NewsController@showNews') }}');
             }, 1000);
            });
           </script>

web.php

Route::group(['middleware'=>['web']], function(){
    Route::get('/article/{id}', 'NewsController@showNews')->name('article');

});



//comments
Route::post('comments/{news_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);

错误消息:

  

缺少[Route:article] [URI:article / {id}]所需的参数。 (视图:D:\ coindeoro_admin \ adminPanel \ resources \ views \ coin \ shownews.blade.php)

1 个答案:

答案 0 :(得分:0)

错误很明显不是吗?您的控制器函数期望一个$news_id参数起作用。

只需在您的路线中传递新闻ID:

$('#commentarea').load('{{ action('NewsController@showNews', ['news_id' => $news->id]) }}');