Prior to assembly of vue, first solve logic code, and then use the reconstructed vue.
Users can refer to concerns about:
Laravel 6 Tutorial: Build a Follow UnFollow System in PHP from Scratch
Recommended expansion pack: Laravel the Follow System 5
Laravel 6 | Follow Unfollow System Example From Scratch
1. To establish the user's attention to user tables followers
Excuting an order:
1 php artisan make:migration create_followers_table
****_create_followers_table.php:
1 <?php 2 3 use Illuminate\Database\Migrations\Migration; 4 use Illuminate\Database\Schema\Blueprint; 5 use Illuminate\Support\Facades\Schema; 6 7 class CreateFollowersTable extends Migration 8 { 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('followers', Function (the Blueprint Table $) { . 17 $ the table-> bigIncrements (' id '); 18 is $ the table-> unsignedBigInteger (' user_id ') -> index () -> Comment ( " user id of attention ") ; . 19 $ the table-> unsignedBigInteger ( ' follower_id ') -> index () -> Comment ( " fan user ID "); 20 is $ the table-> timestamps (); 21 is }); 22 is } 23 is 24 / ** 25 * Reverse the migrations. 26 * 27 * @return void 28 */ 29 public function down() 30 { 31 Schema::dropIfExists('followers'); 32 } 33 } 34 35
Then execute:
1 php artisan migrate
2. Add to User.php association model [in]
1 / ** user fan 2 * @return \ Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany . 3 * / . 4 public function followers () . 5 { . 6 // A foreign key is a fan of ID . 7 return $ this-> belongsToMany (Self class ::, 'followers', 'follower_id', 'user_id') -> withTimestamps (); . 8 } . 9 10 . 11 oF / ** concern to the user 12 is * @return \ Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany 13 is * / 14 public function following () 15 { 16 // is a foreign key of the ID . 17 return $this->belongsToMany(self::class, 'followers', 'user_id', 'follower_id')->withTimestamps(); 18 } 19