
How to implement toggleLike() method in Overtrue\LaravelLike laravel package
How to implement toggleLike() method in OvertrueLaravelLike laravel package
In this tutorial, we will try to find the solution to "How to implement toggleLike() method in OvertrueLaravelLike laravel package" through programming.
You can implement toggleLike() method in the Overtrue\LaravelLike laravel package. You can simply use toggleLike() method on the post which you want to like if it is not already like by logged in user and remove it from like if it is already liked by the user.-
Add like and remove like from post using toggle method in Overtrue\LaravelLike laravel package
public function likePost(Request $request){ $post = Post::find($request->id); $response = auth()->user()->toggleLike($post); return response()->json(['success'=>$response]); }
This toggleLike() method is used in Overtrue\LaravelLike laravel package to add like if the post is not liked by the logged-in user and remove like from the post if the user already liked the post.
-
Increment/decrement like counter in view file on ajax using Overtrue\LaravelLike package in laravel
//Like counter <i class="fas fa-thumbs-up {{ auth()->user()->hasLiked($post) ? 'like-post' : '' }}"></i> <div id="like{{$post->id}}-bs3"> {{ $post->likers()->get()->count() }} </div> //Like anchor tag <a href="javascript:void(0)" class="like-post-btn" data-id="{{ $post->id }}"> <i class="fas fa-thumbs-up {{ auth()->user()->hasLiked($post) ? 'liked-post' : '' }}"></i>Like </a> //Call ajax on click like button (use jQuery) <script> $('.like-post-btn').click(function(){ var id = $(this).data('id'); var c = $('#like'+id+'-bs3').html(); var cObjId = id; var cObj = $(this); $.ajax({ type:'POST', url:'/like-post', data:{id:id}, success:function(data){ if($.isEmptyObject(data.success)){ $('#like'+cObjId+'-bs3').html(parseInt(c)-1); $(cObj).find('.fas.fa-thumbs-up').removeClass("liked-post"); }else{ $('#like'+cObjId+'-bs3').html(parseInt(c)+1); $(cObj).find('.fas.fa-thumbs-up').addClass("liked-post"); } } }); }); </script>
You can display like counter in view file using jquery method after call ajax on click like button.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Laravel create table migration with model
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- Trying to access array offset on value of type null error in laravel
- Laravel recursive function in controller
- How to display HTML tags In Laravel blade
- Method Illuminate\Events\Dispatcher::fire does not exist
- Submit form without CSRF token in Laravel
- How to add active class to menu item in laravel
- How to automatically update the timestamp of parent model in Laravel
- Ajax GET request in laravel
- How to validate URL with https using regex in laravel
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to get data from two tables in laravel
- Update last created record in Laravel
- How to create event and listener in laravel ?
- Call to a member function update() on null
- If condition in Laravel 9
- Display data in table using foreach in Laravel
- How to upload image in laravel 8
- Get Array of IDs from Eloquent Collection
- Convert input array to comma-separated string in laravel controller
- Laravel 9 route group with controller
- How to create pivot table in laravel using migration
- Add class to body in laravel view