Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Construyendo un botón AJAX en seguir laravel

vista de mi hoja

Estaba trabajando para actualizar el botón de seguir en tiempo real, así que noté que si hacía clic en dejar de seguir automáticamente, se actualizaba para seguir, pero cuando hago clic en seguir, no hace nada hasta que actualizo la página manualmente.

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

Mi controlador

 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 answers
Answer question

0

Prueba este:

 $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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!