Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Building an AJAX button on follow laravel

my blade view

I was working on updating the follow button in real-time so I noticed if I clicked on unfollow automatically it update to follow, but when i click on follow it's does nothing until I refresh the page manually

<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);
              }
           }
        });
    });

});

My Controller

public function follwUserRequest(Request $request){

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

        return response()->json(['success'=>$response]);
    }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this one:

  $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);
        }
    }
});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda