【问题标题】:Laravel auto update div contents using ajaxLaravel 使用 ajax 自动更新 div 内容
【发布时间】:2019-11-21 00:03:09
【问题描述】:

我在更新 laravel 项目中的 div 内容时遇到了一些错误。我使用setInterval 方法。但是当我把我的路线放在负载参数中时,它会抛出一个错误。有人可以知道我为什么会收到此错误吗?

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)

【问题讨论】:

  • 您需要传递 id 因为您在操作路线中使用参数

标签: php laravel


【解决方案1】:

错误很明显不是吗?您的控制器函数需要 $news_id 参数才能工作。

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

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

【讨论】:

  • 它给我一个像这样的错误先生。 syntax error, unexpected ')', expecting ']' (View: D:\coindeoro_admin\adminPanel\resources\views\coin\shownews.blade.php) 但是当我修复这样的语法时$('#commentarea').load('{{ action('NewsController@showNews', ['news_id' =&gt; $news-&gt;getKey()]) }}'); 它又抛出了另一个错误Call to undefined method stdClass::getKey()
  • 这是一个简单的语法错误..我错过了],如果你使用任何IDE,你应该已经注意到了
  • 即使我添加了一个右括号先生,它也会给我一个错误Call to undefined method stdClass::getKey()
  • 您的 $news 变量不是 Eloquent 模型?如果是这样,请使用$news-&gt;id
  • 如果我使用 $news-&gt;id 先生,我的网站会变得混乱
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多