vuejs知乎_Laravel Vuejs 实战:开发知乎 (22)关注用户

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值