😁Laravel - Follow Unfollow System Example, like, subscribe full (ok)
https://www.itsolutionstuff.com/post/laravel-5-follow-unfollow-system-exampleexample.html
Source code practive
Chú ý từ bản overtrue/laravel-follow ^3.0.0 nó đã tách thành các gói riêng biệt như laravel-like, laravel-favorite, laravel-subscribe, laravel-vote, laravel-follow (ok) về cơ bản giống nhau nhưng do hoàn cảnh ngữ nghĩa họ tách ra
Like: overtrue/laravel-like
Favorite: overtrue/laravel-favorite
Subscribe: overtrue/laravel-subscribe
Vote: overtrue/laravel-vote
Follow: overtrue/laravel-follow
C:\xampp82\htdocs\lva1\routes\api.php
<?php
use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/user', function () {
// $user1 = User::find(1);
// $user2 = User::find(2);
// $user2->follow($user1);
// // $user2->unfollow($user1);
// return response()->json($user2);
// $user = User::find(1);
// $post = Post::find(2);
// $user->like($post);
// return response()->json($user);
// $user = User::find(1);
// $post = Post::find(2);
// $user->favorite($post);
// return response()->json($user);
// $user = User::find(1);
// $post = Post::find(2);
// $user->subscribe($post);
// return response()->json($user);
$user = User::find(1);
$idea = Post::find(2);
$user->vote($idea);
return response()->json($user);
});
Ví dụ 1: tạo follow, unfollow 👍
Chú ý: version "overtrue/laravel-follow": "^5.1" có chút thay đổi và không sử dụng table user_follower nữa.
C:\xampp82\htdocs\lva1\app\Models\User.php
Cài đặt hiện tại composer require overtrue/laravel-follow nó chưa tương thích với bản 8.x do đó ta dùng cách sau
config/app.php
C:\xampp\htdocs\reset\routes\api.php
C:\xampp\htdocs\reset\database\migrations\2020_04_04_000000_create_user_follower_table.php
C:\xampp\htdocs\reset\app\Models\User.php
Create Dummy Users
Kết quả sau khi truy cập


Ví dụ 2: Tạo like, unlike
Chú ý: Từ version "overtrue/laravel-like": "^5.2" đã thay đổi
C:\xampp82\htdocs\lva1\app\Models\User.php
C:\xampp\htdocs\reset\database\migrations\2018_12_14_000000_create_likes_table.php
C:\xampp\htdocs\reset\database\factories\PostFactory.php
C:\xampp\htdocs\reset\app\Models\Post.php
C:\xampp\htdocs\reset\routes\api.php
C:\xampp\htdocs\reset\database\seeders\DatabaseSeeder.php
C:\xampp\htdocs\reset\app\Models\User.php
C:\xampp\htdocs\reset\database\migrations\2022_05_15_194934_create_posts_table.php
Kết quả:


Ví dụ 3: Tạo favorite, unfavorite
Chú ý "overtrue/laravel-favorite": "^5.1"

C:\xampp82\htdocs\lva1\app\Models\User.php
C:\xampp82\htdocs\lva1\app\Models\Post.php
Ví dụ 4: Tạo subscribe, unsubscribe
C:\xampp82\htdocs\lva1\app\Models\User.php
C:\xampp82\htdocs\lva1\app\Models\Post.php
Ví dụ 5: Tạo vote, unvote

Do họ chưa làm lệnh này nên chúng ta copy thủ công bảng ra migrate
C:\xampp82\htdocs\lva1\app\Models\User.php
C:\xampp82\htdocs\lva1\app\Models\Post.php
Laravel - Follow Unfollow System Example
By Hardik Savani June 16, 2018 Category : PHP Laravel Bootstrap jQuery MySql AjaxPauseUnmuteLoaded: 1.00%Fullscreen
Hi Guys,
Today I have a special tutorial for you developer, I would like to share with you how to implement a follow and unfollow system with PHP Laravel and MySQLi like Twitter and Facebook. So basically, a user can follow unfollow another user and you can see which users following you and how many followers you have.
So, in this post. I will explain you step by step create follow system in laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application. we will use "overture/laravel-follow" composer package for following a system. we will create users table and user authentication using laravel auth. then a user can log in and see how many he has followers and following you.
Just follow a few step and you will get layout like as bellow preview and also you can download script from bellow link.
Preview Of All Users:

Preview Of User Follower:

Preview Of User Following:

Step 1: Install Laravel 5.6
In first step, If you haven't installed laravel 5.6 in your system then you can run bellow command and get fresh Laravel project.
Step 2: Install laravel-follow Package
Now we require to install laravel-follow package for like unlike system, that way we can use it's method. So Open your terminal and run bellow command.
Now open config/app.php file and add service provider and aliase.
config/app.php
After that we need to run migration configure file that we it will automatic generate migrations. so let's run.
Then just migrate it by using following command:
Read Also: Laravel 5 import export to excel and csv using maatwebsite example.
Step 3: Create Authentication
In this step we require to create authentication of Laravel 5.6, so laravel provide artisan command to create authentication that way we don't require to create route and controller for login and registration. so run bellow command:
Step 4: Create Dummy Users
In this step, we will create some dummy users for testing, so we will create dummy users using formate factory. so run below command:
Step 5: Update User Model
here we need to update User model. we need to use CanLike facade in User model. So let's update as bellow code.
App/User.php
Step 6: Add Routes
In this step, we will create routes for like unlike system. So we require to create following route in web.php file.
routes/web.php
Step 7: Create Controller Method
now in HomeController, we will add three new method users(), user() and ajaxRequest(). so let's see HomeController like as bellow:
app/Http/HomeController.php
Step 8: Create Blade files and JS File
Now in this file we will need to create userList.blade.php, users.blade.php and usersView.blade.php files and custom.js file. So let's create both files.
resources/views/users.blade.php
resources/views/usersView.blade.php
resources/views/userList.blade.php
publis/js/custom.js
Read Also: PHP Laravel - Like Dislike System Tutorial
Now you are ready to run full example.
You can also download full source code of this example.
I hope it can help you....
Last updated
Was this helpful?