Laravel Ajax更新变量

时间:2016-05-13 07:31:53

标签: laravel-5.2

        <div class="panel panel-primary">
            <div class="panel-heading">Add user Status</div>

        <form action="{{ route('post.create') }}" method="post">
            <div class="panel-body">
            <div class="form-group">
                <textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
            </div>
                </div>
            <div class="panel-footer clearfix">
                <button class="btn btn-info pull-right"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></i> Post</button>


            </div>

            <input type="hidden" value="{{ Session::token() }}" name="_token">
        </form>
            </div>

            <div id="main" class="main">

                <header><h3>What other people say...</h3></header>
                @foreach($top_15_post as $status)
                    {!!
            view ('layouts.app-internal.user-status-layaout',[
            'status' =>$status,
            'user'   =>\App\User::find($status->user_id),
            'comments'=>\App\Comment::where('status_id',$status->id)->get(),
             'countcomment'=>\App\Comment::where('status_id',$status->id)->count(),
            'countlike'=>\App\Like::where('like',1)->where('post_id',$status->id)->count() ,
            'countdislike'=>\App\Like::where('like',0)->where('post_id',$status->id)->count(),



            ])->render()
            !!}
                @endforeach
            </div>
        </div>
            </div>

        </div>
        </div>
                </div>
            <!-- Right part -->


        </div>
     </div>
    </div>

这是我在laravel的home.blade.php。我只是简单地实现了一个帖子文本系统,用户可以对帖子进行评论和喜欢。我将上面的变量传递给user-status-layaout.blade.php,它会显示评论和喜欢以及帖子。 这是我的user-status-layaout.blade.php。我喜欢和不喜欢的功能正常工作。但是我希望使用ajax更新像count一样。如何使用ajax立即更新countlike和countdislike变量?

<div class="panel panel-default">
<div class="panel-heading">{{  $status->user->name  }} Posted on {{ $status->created_at }}</div>
<div class="panel-body">
    <div class="row">
        <div class="col-md-1">
            <img src="{{Auth::user()->getavatar()}}" class="img-responsive">
        </div>
        <div class="post" data-postid="{{ $status->id }}">






            <p>{{ $status->body }}</p>



            <div class="interaction">
                <a href="#" class="like">{{ Auth::user()->likes()->where('post_id', $status->id)->first() ? Auth::user()->likes()->where('post_id', $status->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like'  }}</a> |
                <a href="#" class="like">{{ Auth::user()->likes()->where('post_id', $status->id)->first() ? Auth::user()->likes()->where('post_id', $status->id)->first()->like == 0 ? 'You don\'t like this post' : 'Dislike' : 'Dislike'  }}</a>

                @if(Auth::user() == $status->user)
                    |
                    <a href="#" class="edit">Edit </a>|


                    <form method="get" action="{{ route('post.delete', ['post_id' => $status->id]) }}" accept-charset="UTF-8" style="display:inline">
                        <button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDelete" data-title="Delete Post" data-message="Are you sure you want to delete this post ?">
                            <i class="glyphicon glyphicon-trash"></i> Delete
                        </button>
                    </form>


                @endif




                <ul class="list-unstyled list-inline ">
                    <li>

                        <button class="btn btn-xs btn-info"  type="button" data-toggle="collapse" data-target="#view-comments-{{$status->id}}" aria-expanded="false" aria-controls="collapseExample">
                            <i class="fa fa-comments"></i>View & Comment </button>
                    </li>

                    <li>{{$countcomment}} comments</li>

                    <li >{{$countlike}} likes</li>

                    <li>{{$countdislike}} dislikes</li>

                </ul>


            </div>



        </div>

    </div>
</div>
<div class="panel-footer clearfix">
    <div class="form-group">
        <form action="{{ route('comment.create') }}" method="post">


            <div class="input-group">
                <input type="text" class="form-control" name="comment-text" id="comment-text" placeholder="Add Comment">
                                        <span class="input-group-btn">
                                            <button class="btn btn-info btn-xs"   type="submit"><span class="glyphicon glyphicon-plus"></span>Add </button>
                                        </span>
                <input type="hidden" value="{{ $status->id }}" name="id" id="id">
            </div><!-- /input-group -->
            <input type="hidden" value="{{ Session::token() }}" name="_token">
        </form>



    </div>

    <div class="collapse" id="view-comments-{{$status->id}}">

        @if($comments->first())
            @foreach($comments as $comment)

                <blockquote >
                    <div class="row" >
                        <div class="col-md-1" >
                            <img src="{{\App\User::find($comment->user_id)->getavatar()}}" class="img-responsive">

                        </div>

                        <div class="col-md-11">
                            <ul class=" list-inline list-unstyled ">
                                <li>
                                    <a href="">{{\App\User::find($comment->user_id)->name}}</a>
                                </li>

                            </ul>

                            <div class="comment" data-commentid="{{ $comment->id }}">
                                <p> {{$comment->comment_text}}</p>

                            <div class="interaction1">



                                @if(Auth::user() == $comment->user)

                                    <a href="#" class="editcomment">Edit </a>|


                                    <form method="get" action="{{ route('comment.delete', ['comment_id' => $comment->id]) }}" accept-charset="UTF-8" style="display:inline">
                                        <button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDeleteComment" data-title="Delete Comment" data-message="Are you sure you want to delete this comment ?">
                                            <i class="glyphicon glyphicon-trash"></i> Delete
                                        </button>
                                    </form>


                                @endif
                            </div>
                            </div>


                        </div>
                    </div>
                    <p> posted {{$comment->created_at->diffForHumans()}}</p>
                </blockquote>



            @endforeach
        @else
            <p>This status no comment</p>



        @endif

    </div>



</div>

1 个答案:

答案 0 :(得分:0)

您可以使用WindowTimers.setTimeout()

创建计时器功能
$('some-selector').load(function(){
    function likeTimer() {
        setTimeout(function(){
            // Some JSON request that updates the elements' values.
            likeTimer();
        }, 1000);
    }
});

或者您可以使用WindowTimers.setInterval()

定期轮询服务器
$('some-selector').load(function(){
    setInterval(function() { 
        // Some JSON request that updates the elements' values.
    }, 1000);
});

然后在Laravel中定义一条路径,为您要更新的数据返回JSON。