【问题标题】:Building an AJAX button on follow laravel在关注 laravel 上构建一个 AJAX 按钮
【发布时间】:2021-07-31 14:18:38
【问题描述】:

我的刀片视图

我正在实时更新关注按钮,所以我注意到如果我点击取消关注,它会自动更新关注,但是当我点击关注时它什么都不做,直到我手动刷新页面

<button data-id="{{ $user->id }}" class="w-1/2 px-3 py-2 text-base text-white border rounded-lg action-follow bg-blue-lite hover:bg-blue"> <strong>
    @if (auth()->user()->isFollowing($user))
        UnFollow
    @else
        Follow
    @endif
    </strong>
</button>
$(document).ready(function() {

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('.action-follow').click(function(){
        var user_id = $(this).data('id');
        var cObj = $(this);
        var c = $(this).parent("div").find(".tl-follower").text();

        $.ajax({
           type:'POST',
           url:'/profile/follow',
           data:{user_id:user_id},
           success:function(data){
              console.log(data.success);
              if(jQuery.isEmptyObject(data.data)){
                cObj.find("strong").text("Follow");
                cObj.parent("div").find(".tl-follower").text(parseInt(c)-1);
              }else{
                cObj.find("strong").text("UnFollow");
                cObj.parent("div").find(".tl-follower").text(parseInt(c)+1);
              }
           }
        });
    });

});

我的控制器

public function follwUserRequest(Request $request){

        $user = User::find($request->user_id);
        $response = auth()->user()->toggleFollow($user);

        return response()->json(['success'=>$response]);
    }

【问题讨论】:

  • 告诉我用户型号代码,我猜不出来 toggleFollow
  • @Arash 我正在使用 laravel 关注包。 github.com/overtrue/laravel-follow
  • 所以在我的用户模型中,我使用了打包文档 Followable 中的特征建议

标签: ajax laravel


【解决方案1】:

试试这个:

  $user = User::find($request->user_id);
  $response = auth()->user()->toggleFollow($user);
  $status = auth()->user()->isFollowing($user);

  return response()->json([ 'success' => $response, 'is_following' => $status ]);
<button data-id="{{ $user->id }}" id="mybutton{{ $user->id }}"  class="w-1/2 px-3 py-2 text-base text-white border rounded-lg action-follow bg-blue-lite hover:bg-blue">
    <strong>
    @if (auth()->user()->isFollowing($user))
        UnFollow
    @else
        Follow
    @endif
    </strong>
</button>
$.ajax({
    type:'POST',
    url:'/profile/follow',
    data:{user_id:user_id},
    success:function(data){
        console.log(data);
        if(data.is_following == true){ 
            cObj.find("strong").text("UnFollow");
            cObj.parent("div").find(".tl-follower").text(parseInt(c)+1);
        }else{
            cObj.find("strong").text("Follow");
            cObj.parent("div").find(".tl-follower").text(parseInt(c)-1);
        }
    }
});

【讨论】:

  • @PraiseAdeala 你能分享 console.log 吗?
  • 我的console.log上没有任何错误,我之前没有分享完整的代码,我的意思是我的完整代码仍然可以添加urs
  • @PraiseAdeala 可以分享/profile/follow功能码吗?
  • 我已经发布了控制器,请检查
  • 在尝试后取消关注按钮正在工作,但是当我点击关注按钮时它不会更新,直到我刷新页面
猜你喜欢
  • 2014-04-23
  • 2016-02-14
  • 1970-01-01
  • 2018-07-21
  • 2013-12-08
  • 2016-06-20
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多