Laravel使用Ajax / jquery每'x'秒自动刷新div

时间:2019-07-09 08:12:51

标签: php jquery ajax laravel

我试图用laravel创建一个网站,但在理解如何根据设置的时间或更好的方式将内容动态加载到部门中时遇到一些困难。我在ajax中发现了此setInterval方法,但是当我在代码中实现该方法时,我的网站变得混乱,并且滞后并显示了一些错误。有人可以帮我弄这个吗?代码答案非常有用。

shownews.blade.php(其中的注释显示在div commentarea中)

<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('F nS, Y - g:iA' ,strtotime($comment->created_at)) }}</p>
                              </div>
                          </div>
                            <div class="comment-content">
                                    {{$comment->comment}}
                            </div>
                            </div>
                          @endforeach
                      </div>
                  </div>

<script>
function autoRefresh_div() {
    $("#commentarea").load('/article/' + {{$news->id}});
}
setInterval(autoRefresh_div, 5000);
autoRefresh_div();
</script>



<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

CommentsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\News;
use App\Graph;
use Validator;
use Session;




class CommentsController extends Controller
{
    public function store(Request $request, $news_id)
    {
        //
        $this->validate($request, array(
            'name'=> 'required | max:255',
            'email'=> 'required| email | max:255',
            'comment'=> 'required | min:5'
        ));

        $news = News::find($news_id);

        $comment = new Comment();

        $comment->name = $request->name;
        $comment->email = $request->email;
        $comment->comment = $request->comment;
        $comment->approved = true;
        $comment->news()->associate($news);

        $comment->save();
        // return response()->json($comment);

        return redirect()->route('article', [$news->id]);

    }
}

NewsController.php

<?php

namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;
use App\News;
use Validator;
use Image;
use View;
use Storage;
use Illuminate\Support\Facades\Input;
// use App\Http\Controllers\Controller;

class NewsController extends Controller
{


    //I did not include the codes here becauase it is for the admin to add news 


    public function showNews($id)
    {

        $all = DB::table('news')->get();
        $news = News::find($id);
        return View::make('coin.shownews', compact('news','all'));
    }
}

0 个答案:

没有答案