在vue组件化之前,先解决逻辑上的代码,然后再用vue重构。
1.建立用户关注用户的表 followers
执行命令: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 (Blueprint $table) {
17 $table->bigIncrements('id');
18 $table->unsignedBigInteger('user_id')->index()->comment("被关注的用户的id");
19 $table->unsignedBigInteger('follower_id')->index()->comment("粉丝用户的id");
20 $table->timestamps();
21 });
22 }
23
24 /**
25 * Reverse the migrations.
26 *
27 * @return void
28 */
29 public function down()
30 {
31 Schema::dropIfExists('followers');
32 }
33 }
34
35
_create_followers_table.php
接着执行:1 php artisan migrate
2.模型关联 【添加到User.php中】1 /** 用户的粉丝
2 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
3 */
4 public function followers()
5 {
6 //外键是粉丝的id
7 return $this->belongsToMany(self::class, 'followers', 'follower_id', 'user_id')->withTimestamps();
8 }
9
10
11 /** 用户关注的作者
12 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
13 */
14 public function following()
15 {
16 //外键是作者的id
17 return $this->belongsToMany(self::class, 'followers', 'user_id', 'follower_id')->withTimestamps();
18 }
19
标签:Laravel,function,知乎,return,22,用户,followers,table,id
来源: https://www.cnblogs.com/dzkjz/p/12393041.html